I am trying to create a small program where I can have the program read in a word. If the word has 6 letters, display the word backwards. If not, tell the user how many letters the word has. Do this until the user enters “japan”.
The example output is:
Enter a word > chess
chess has 5 letters
Enter a word > google
Magic six! ELGOOG
Enter a word > japan
Goodbye!
The if statement and the for loop are bypassed for some reason and I'm not sure why. My current code is as such:
import java.util.*; //imports the utilities
public class WordPyramid {
public static void main(String[] args) {
String n;
Scanner kb = new Scanner(System.in);
System.out.println("Enter a word: ");
while ((n = kb.nextLine().toLowerCase()) == "japan" ) {
int y = n.length();
{
if (y == 6) {
String reverse = "";
for (int i = y - 1; i >= 0; i--) {
reverse = reverse + n.charAt(i);
System.out.println(reverse);
}
}
else {
System.out.println(n+" has "+ y+1 +" letters ");
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire