在这种情况下我将如何重置实例变量?我是 java 的新手,所以请原谅我,如果这是一个简单的答案
How would I reset the instance variable in this situation? I'm new to java so forgive me if it's a simple answer
不知道叫什么,但我很确定运行代码的那个是主要的,也就是本文下面的那个。此外,我正在使用一个名为 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 + "\"";
}
}
不知道叫什么,但我很确定运行代码的那个是主要的,也就是本文下面的那个。此外,我正在使用一个名为 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 + "\"";
}
}