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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...