toString 方法不大写字母

0 java string methods tostring

我正在为一个类学习 Java,我需要做的一项任务是在我的代码中实现 String 方法。在提示用户设置文本后,我使用了一个 toLowerCase() 方法并将其打印出来。在另一行中,我使用了 toUpperCase() 方法并打印了它。两者都打印正确,但每当我使用 toString() 方法时,它都只显示我的小写文本。

这是我的主要课程:

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);

    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的课程和我所有的方法

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()
    {
        text = text.toUpperCase();
        return text;
    }

    // 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()
    {
        text = text.toLowerCase();
        return text;
    }

    // Reset the instance variable to the new text
    public void setText(String newText)
    {
        text = newText;
    }

    // Returns a String representatin 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()
    {
        text = text;
        return "I say, " + "\"" + text + "\"";
    }
}
Run Code Online (Sandbox Code Playgroud)

我为长粘贴和我糟糕的英语道歉。

Cod*_*der 6

这是因为您修改了 的值text。即使你回来,它也会一直存在。你不应该。相反,直接返回如下:

String yell() {
    return text.toUpperCase();
}
Run Code Online (Sandbox Code Playgroud)