我试图编写一个程序,该程序需要一个字符串,将其分解为字符,并向后打印每个字符。我想看看问题是什么,我只是不知道该如何解决。这里是我的代码:向后打印字符串的Java程序
public static void main(String[] args) {
//takes a string and prints all the letters backwards all on one line
String fruit = "apple";
backwards(fruit);
}
public static void backwards(String theFruit) {
int length = theFruit.length() - 1;
int counter = 0;
while(length > counter) {
char theCharacter = theFruit.charAt(length);
System.out.println(theCharacter);
counter++;
}
}
出于某种原因,它只是打印所有的一个字母,我不知道为什么。
长度永远不会改变,你总是输出char'length' –