"Character.toUpperCase" 的结果被忽略

Result of "Character.toUpperCase" is ignored

这是我的第一个问题,我迫不及待地想问这个问题,因为我还没有找到与我的问题相关的任何其他帖子,或者相关但太复杂且与实际问题无关的帖子我有代码。

问题是我想要求用户输入,输入的字母应该倒置,例如:Hello to hELLO,反之亦然。但是出现警告"Result of 'Character.toUpperCase()' is ignored",知道如何解决吗?

for (int i = 0; i < word.length(); i++)
{
    if (Character.isLowerCase(word.charAt(i)))
    {
        Character.toUpperCase(word.charAt(i));
    }
    else
    {
        Character.toLowerCase(word.charAt(i));
    }
}

您好,欢迎来到 Stack overflow。 问题是 Character.toUpperCase() 不会覆盖字符串中的字符。

public static void main(String[] args) {
    String word = "Hello";
    String wordInverted = "";

    for (int i = 0; i < word.length(); i++)
    {
        //Get the char as a substring
        String subChar = word.substring(i, i+1);

        if (Character.isUpperCase(word.charAt(i)))
        {
            subChar = subChar.toLowerCase();
        }
        else
        {
            subChar = subChar.toUpperCase();
        }

        wordInverted += subChar; //Add the newly inverted character to the inverted string
    }

    System.out.println(wordInverted);
}