I'm trying to write a program that takes a string, breaks it up into characters, and prints each character backwards. I sort of see what the problem is, I just am not sure how to fix it. Here is my code:
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++;
}
}
For some reason it just prints all one letter and I am not sure why.
Aucun commentaire:
Enregistrer un commentaire