gpt4 book ai didi

java - "Character.toUpperCase"的结果被忽略

转载 作者:行者123 更新时间:2023-12-02 09:38:03 27 4
gpt4 key购买 nike

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

问题是我想要求用户输入,并且输入的字母应该颠倒,例如:Hello to hELLO,反之亦然。但出现警告“'Character.toUpperCase()'的结果被忽略”,知道如何解决吗?

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

关于java - "Character.toUpperCase"的结果被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57337848/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com