Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有try块的无限循环-java_Java_Loops_Try Catch - Fatal编程技术网

带有try块的无限循环-java

带有try块的无限循环-java,java,loops,try-catch,Java,Loops,Try Catch,当运行时,它会正确捕获异常,但当我选择一个案例时,它会导致一个无限循环。我已经试了几个小时寻找解决方案,但没有任何效果 int whatYouWant = 1; boolean contin = true; while (whatYouWant != 0) { while (contin) { try { Scanner scanner = new Scanner(System.in); System.out.println

当运行时,它会正确捕获异常,但当我选择一个案例时,它会导致一个无限循环。我已经试了几个小时寻找解决方案,但没有任何效果

int whatYouWant = 1;
boolean contin = true;
while (whatYouWant != 0) {
    while (contin) {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Which program would you like to run?" + " Input 0 to exit the program.");
            whatYouWant = scanner.nextInt();
            contin = false;
            scanner.close();
        } catch (Exception e) {
            System.out.println("Try again.");
            contin = true;
        }
    }
    AllCode a = new AllCode();
    switch (whatYouWant) {
    case 1:
        // method call that calls a method with another try catch block
        break;
    case 2:
        // another method call
        break;
    case 3:
        // another method call and so on and so forth
        break;
    case 0:
        whatYouWant = 0;
        break;
    default:
        System.out.println("Please try again.");
        break;
    }
}
下面是交换机调用的方法的示例:

void multiplytheNumbers() {// Using the parameter to

    boolean run = true;
    while (run) {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the 1st number:");
            int num1 = sc.nextInt(); /// getting user input from the user.
            System.out.println("Enter the 2nd number:");
            int num2 = sc.nextInt(); /// getting more user input
            int product = num1 * num2; /// using the multiplication operator
            int sum = num1 + num2; /// using then addition operator
            int diff = num1 - num1; /// using the subtraction operator
            int quotient = num1 / num1; /// using the quotient operator
            System.out.println(sum);
            System.out.println(diff);
            System.out.println(product);
            System.out.println(quotient);
            num1 = num1++; // using the increment operator to increase num1
                            // by 1
            num2 = num2--; // using the decrement operator to decrease num2
                            // by 1
            run = false;
            sc.close();
        } catch (Exception e) {
            System.out.println("Invalid Input. Try again.");
        }
    }
}

我在您的
开关中读取的是
defualt
而不是
default
,这是因为如果
Scanner.nextInt()
与int不匹配,它会引发异常,但如果调用
Scanner.next()
,您将看到前面输入的相同字符串。因此,每次进入循环时,它都会尝试将相同的字符串输入解析为整数,并每次抛出异常

您可以通过以下代码注意到这种行为:

Scanner scanner = new Scanner(System.in);
while (contin) {
    try {
        System.out.println("Which program would you like to run?" + " Input 0 to exit the program.");
        whatYouWant = scanner.nextInt();
        contin = false;
    } catch (Exception e) {
        System.out.println(scanner.next());
        System.out.println("Try again.");
        contin = true;
    }
}
scanner.close();
您会注意到,当调用
scanner.next()
时,它仍然会扫描相同的字符串


为了快速解决问题,您应该尝试
Integer.parseInt(scanner.next())
代替
scanner.nextInt()

哪一个循环是无限的?不要将try-catch用作控制流的主要部分。如果您想防止用户提供不正确的值,只需检查扫描器是否有NEXTINT
(如果扫描器没有通知用户并使用
next
)使用不正确的令牌)。更多信息:(可能重复)。我建议您使用调试器逐步检查代码,并了解为什么它会进入无限循环。使用调试,你应该能够在几分钟内解决这个问题,一旦你知道如何使用它。是的,在我的实际程序中它很好,我只是重写了switch语句,这是一个打字错误。绝对不是问题。这应该是一个评论。即使这是一个问题,关于拼写错误的问题在堆栈溢出上也是离题的,所以帮助OP的正确方法是将其作为评论发布。如果你把它贴成answer ant,它将获得投票或被接受。自动清理脚本将不再删除这样的问题。你能详细说明一下吗?我在我的系统上试过了,它成功了!