我最近开始学习Java,在测试一些东西时遇到了一个问题。这可能是一个很简单的问题,但我似乎无法解决它。 这是我的代码:
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);
我希望当您输入1、2或3时,会得到一个字符串提示您再次输入数字并接受用户输入,而当您输入4时,循环将中断并转到“完成”字符串。如果您能用简单的代码帮助我解决这个问题,我将不胜感激,因为我不知道更高级的东西。