下面的代码在try catch catch捕获异常后终止。不允许我从菜单选项中进行选择。所以我的问题是我必须对此代码进行哪些更改,以便我可以循环,以便我可以再次获得用户输入。程序在switch语句中使用try catch块后终止
public class Main {
public static void main(String[] args) {
Modify modifyObj = new Modify();
int choice = 0 ;
Scanner input = new Scanner(System.in);
//begin loop
do {
try{
//display menu
System.out.println("Choose one option from following option available: ");
System.out.println("0) Exit program. ");
System.out.println("1) Create a Roster");
System.out.println("2) Modify a Roster");
System.out.println("3) Delete a Roster");
choice = input.nextInt(); //gets user input
switch (choice) {
case 1:
//code
break;
case 2:
//code
break;
case 3:
//code
break;
}// end of switch statement
break;
}//end oftry
catch(InputMismatchException inputMismatchException){
System.out.println("Enter integer value between 0 and 7:");
continue;
}
}while (choice!=0); //loop until user exit 0.
}//end of main
}// end of Main class
为什么有'打破;'了'开关结束后(选择)'块?这不会将你踢出循环吗? – KevinO
这是我的代码中的错字,我只是改变仍然有同样的问题。 – user6238843
它完全停止。 – user6238843