Cannot Figure out how to catch InputMismatchException

前端 未结 2 2038
一整个雨季
一整个雨季 2021-01-26 12:23

So here is my current code for catching an InputMismatchException error

int weapon = 0
   boolean selection = true;
   while(selection) {
    try {
      System.         


        
2条回答
  •  旧时难觅i
    2021-01-26 13:05

    Try this:

    int weapon = 0;
       do{
           System.out.println("Pick number 1, 2, or 3.");
           if(scan.hasNextInt()){
               weapon = scan.nextInt();
               break;
           }else{
               System.out.println("Enter an integer only");
               scan.nextLine();
           }
       }while(true);
    

    This will make sure it is an integer and it will keep asking until it gets it.

提交回复
热议问题