我有一个任务,我需要让代码接受用户输入的单词,将其变成大写字母,反转并输出它。到目前为止,这是我的代码,但我真的不知道如何做到这一点。
import java.util.Scanner;
public class WordSizeChecker {
public static void main(String[] args) {
Scanner kb = new Scanner (System.in);
System.out.print("Please enter a word: ");
String oword = kb.nextLine();
String word = oword.toUpperCase();
int length = word.length();
int i = 0;
while (i < length)
{
System.out.println(word.substring(i,length));
length--;
}
}
}
输出:
Please enter a word: chess
CHESS
CHES
CHE
CH
C
最佳答案
new StringBuilder(oword).reverse().toString().toUpperCase();
或者使用循环
oword = oword.toUpperCase();
for(int i = oword.length() - 1; i >= 0; i--)
{
System.out.print(oword.charAt(i));
}
System.out.println();
关于java - 如何在java中使用while循环反转字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35247768/