我试图在 java 上做一个健壮的代码,但它似乎不起作用。我正在寻找的是用户输入输入,如果不是必需的输入,程序将检查输入,然后用户将可以选择重新输入适当的输入,直到输入与所需的输入匹配,或者干脆退出。这是我到目前为止所拥有的。当我运行此代码时,一切正常,除非用户输入错误并想退出。即使用户重新输入正确的输入或退出,while 循环也会继续运行并且不会停止。我怎样才能使这项工作?
//question
System.out.println("Summer, Winter, Fall, or Spring");
System.out.print("Which season is your favarite? ");
String favSeason = in.next();
System.out.println();
//Control the inputs by converting them to Upper Case
String favSeasonInput = favSeason.toUpperCase();
//required answers of the question
String seasons = "SUMMER, WINTER, FALL, SPRING?";
String quit = "QUIT!";
boolean isSeasons = (favSeasonInput.equals(seasons.substring(0, 6)) ||
favSeasonInput.equals(seasons.substring(8, 14)) ||
favSeasonInput.equals(seasons.substring(16, 20)) ||
favSeasonInput.equals(seasons.substring(22, 28)));
boolean isQuit = favSeasonInput.equals(quit.substring(0, 4));
//inialize variables that will compute scores
int favSeasonScore = 0;
//if user enters an input otherthan seasons
while (!isSeasons){
favSeason = in.next();
if(isQuit){
System.exit(0);
}
}
//Conditions to set up scores for seasons
if(favSeasonInput.equals(seasons.substring(0, 6))){
favSeasonScore = 6;
System.out.println("Summer is " + favSeasonScore + " points");
}
else if(favSeasonInput.equals(seasons.substring(8, 14))){
favSeasonScore = 14;
System.out.println("Winter is " + favSeasonScore + " points");
}
else if(favSeasonInput.equals(seasons.substring(16, 20))){
favSeasonScore = 20;
System.out.println("Fall is " + favSeasonScore + " points");
}
else if(favSeasonInput.equals(seasons.substring(22, 28))){
favSeasonScore = 28;
System.out.println("Spring is " + favSeasonScore + " points");
}
System.out.println(favSeasonScore);