在这种情况下,我将如何重置实例变量?我是Java的新手,所以如果这是一个简单的答案,请原谅我



不知道调用了什么,但我很确定运行代码的是main,这将是本文下面的这个。此外,我正在使用一种名为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 + """;
}
}

最新更新