java - 在这种情况下我将如何重置实例变量?我是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;
}