如何解决在这种情况下,如何重置实例变量?我是Java的新手,所以请简单回答一下
不知道有什么叫什么,但是我很确定运行代码的是主要代码,而这就是本文下面的代码。另外,我正在使用一种称为codeHS的在线工具,因此,如果它不是完全正确的格式,即使它能正常工作也不会接受我所做的事情。
import java.util.Scanner;
public class TalkerTester
{
public static void main(String[] args)
{
Scanner input = new Scanner(system.in);
System.out.println("Enter some text: ");
String words = input.nextLine();
Talker talky = new Talker(words);
String yelling = talky.yell();
String whispers = talky.whisper();
System.out.println(talky);
System.out.println("Yelling: " + yelling);
System.out.println("Whispering: " + whispers);
}
}
在这之下是完成所有工作的另一部分,这就是问题所在。以上部分是事先提供给我们的,因此我不允许更改。如果其他任何地方有问题,也请通知我。
public class Talker
{
private String text;
// Constructor
public Talker(String startingText)
{
text = startingText;
}
// Returns the text in all uppercase letters
// Find a method in the JavaDocs that
// will allow you to do this with just
// one method call
public String yell()
{
return text.toupperCase();
}
// Returns the text in all lowercase letters
// Find a method in the JavaDocs that
// will allow you to do this with just
// one method call
public String whisper()
{
return text.toLowerCase();
}
// Reset the instance variable to the new text
public void setText(String newText)
{
}
// Returns a String representation of this object
// The returned String should look like
//
// I say,"text"
//
// The quotes should appear in the String
// text should be the value of the instance variable
public String toString()
{
return "I say,\"" + text + "\"";
}
}
解决方法
// i am thinking you want to set the text to newText and this very much is borther.
public void setText(String newText) {
// By default a new string created and it is not the reference hope i
// can what you want to say want anything else info please reply.
this.text = newText;
}
,
我在标题中说的一个非常基本的答案,谢谢您的帮助
public class Talker
{
private String text;
// Constructor
public Talker(String startingText)
{
text = startingText;
}
// Returns the text in all uppercase letters
// Find a method in the JavaDocs that
// will allow you to do this with just
// one method call
public String yell()
{
return text.toUpperCase();
}
// Returns the text in all lowercase letters
// Find a method in the JavaDocs that
// will allow you to do this with just
// one method call
public String whisper()
{
return text.toLowerCase();
}
// Reset the instance variable to the new text
public void setText(String newText)
{
text = newText;
}
// Returns a String representation of this object
// The returned String should look like
//
// I say,"text"
//
// The quotes should appear in the String
// text should be the value of the instance variable
public String toString()
{
return "I say,\"" + text + "\"";
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。