我的代码有什么问题?崩溃了

时间:2019-10-26 14:00:33

标签: java

当用户输入0或6之类的输入时,程序将进入无限循环。

我试图创建新的while循环,break和Continue语句。但是,它们都不起作用。

public static final Pattern p12345 = Pattern.compile("[1-5]");

public Counter() {
          this.counter = 0;
          // System.in is like System.out, however, for input and not for output.
          Scanner s = new Scanner(System.in);
          /*
           * can take value 1 (add) 2 (subtract) 3 (show counter) 4 (set counter) 5 (exit)
           */
          int topChoice = 0; // Can be anything but 5 to enter the loop
          while (topChoice != 5) {
               System.out.println("Please enter:\n" + "1 to add to the total\n" + "2 to subtract from the total\n"
                         + "3 to show the total\n" + "4 to set the total\n" + "5 to exit the program");

               try {
                    topChoice = Integer.parseInt(s.next(p12345));
               } catch (InputMismatchException e) {
                    System.out.println("You need to enter an integer between 1-5.");
               }
               switch (topChoice) {
               case 1:
                    add(s);
                    break;
               case 2:
                    subtract(s);
                    break;
               case 3:
                    show();
                    break;
               case 4:
                    set(s);
                    break;
               case 5:
                    System.out.println("Finally there are " + counter + " items available.");
                    break;
               default:
                    throw new IllegalArgumentException();
               }
          }
     }

当用户输入一个不在1-5范围内的整数时,我希望回头再次询问,但它崩溃了。

Please enter:
1 to add to the total
2 to subtract from the total
3 to show the total
4 to set the total
5 to exit the program
6
You need to enter an integer between 1-5.
Exception in thread "main" java.lang.IllegalArgumentException
        at Counter.<init>(Counter.java:87)

3 个答案:

答案 0 :(得分:0)

因为这行代码:

 default:
                throw new IllegalArgumentException();

我认为您在IllegalArgumentException的控制台中遇到错误了吗? 在未声明0-6的情况的情况下,switch语句将默认进入默认情况,在该情况下您将引发异常

答案 1 :(得分:0)

您可以进一步简化代码。我的意思是,您可以使用简单的if-else语句检查1到5的范围,而不是try-catch,它会与IllegalArgumentException类一起抛出Patternelse语句可以是控制台打印,说明该数字超出范围。另外,我假设您应该为counter循环的每次迭代将while变量增加1?在您的代码中看不到。

修订:

while (topChoice != 5) {
   counter++;
   try {
      topChoice = Integer.parseInt(s.next());
   }
   catch (NumberFormatException er)
   {
      System.out.println("Error: You must enter in an integer.");
   }
   catch (Exception er)
   {
      System.out.println("Error: " + er.getMessage());
   }
   if (topChoice >= 1 && topChoice <= 5)
   {
       switch (topChoice) {
          case 1:
              add(s);
              break;
          case 2:
              subtract(s);
              break;
          case 3:
              show();
              break;
          case 4:
              set(s);
              break;
          case 5:
              System.out.println("Finally there are " + counter + " items available.");
              break;
       }
   }
   else
       System.out.println("Error. Number must be between 1 and 5.");
}

答案 2 :(得分:0)

您的程序引发异常,因为您告诉它:

throw new IllegalArgumentException();

如果您希望它循环回到顶部,那么在default情况下什么也不要做!您可以一起删除整个default案例。

您需要做的另一件事是实际读取您未能读取的输入。另外,您应该将topChoice重置为0,否则它将最终执行上一次循环运行时的操作。

try {
    topChoice = Integer.parseInt(s.next("[1-5]"));
} catch (InputMismatchException e) {
    System.out.println("You need to enter an integer between 1-5.");
    s.nextLine(); // <----- here
    topChoice = 0; // <---- and here
}

否则,在循环的下一次迭代中,扫描程序将尝试读取之前无法读取的相同输入,然后再次失败,从而创建无限循环。


即使代码在上述修改之后可以工作,您也不应该真正进行带有异常的“控制流”。您应该阅读一行,然后检查它是否与模式匹配:

String line = s.nextLine();
if (line.matches("[1-5]")) {
    topChoice = Integer.parseInt(line);
} else {
    System.out.println("You need to enter an integer between 1-5.");
    topChoice = 0;
}