I recently started studying Java and came across a problem while testing out something. This might be a very easy question, but I can't seem to solve it. This is my code:
int firstj = 1;
if (firstj == 1) {
String choice = "Type a number between 1 and 4";
System.out.println(choice);
while (true) {
if (firstj == 1) {
Scanner third = new Scanner(System.in);
String thirdch = third.nextLine();
while (true) {
if (thirdch.equals("1")) {
System.out.println("Show choice and accept input again ");
System.out.println(choice);
break;
} else if (thirdch.equals("2")) {
System.out.println("Show choice and accept input again ");
System.out.println(choice);
break;
} else if (thirdch.equals("3")) {
System.out.println("Show choice and accept input again ");
System.out.println(choice);
break;
}
else if (thirdch.equals("4")) {
// I need this to break the loop and move on to the
// "Done." string
break;
}
else {
System.out.println("Type a number between 1 and 4");
thirdch = third.nextLine();
}
}
}
}
}
String done = "Done";
System.out.println(done);
I want to make it so that when you type 1, 2 or 3, you get the string telling you to type a number again and accept user input, while when you type 4 the loop breaks and goes to the String done. I would be grateful if you could help me solve this problem with an easy code, since I don't know any of the more advanced things.
Aucun commentaire:
Enregistrer un commentaire