【问题标题】:I put my try catch in a do while loop, but the commands after the do-while loop continue running even though there is an exception caught我把我的try catch放在一个do while循环中,但是do-while循环之后的命令继续运行,即使有一个异常被捕获
【发布时间】:2021-06-16 17:15:28
【问题描述】:

我正在构建一个小程序来检查用户输入是否为数字。程序运行,但是当我的 catch 块捕获异常时,它以某种方式退出了它所在的嵌套 do-while 循环。

这是我的程序:

package TESTCLASS;
import java.util.Scanner;


public class Apples {
    
    static int readValidInt(Scanner in, String prompt, int min,  int max){
            
        int validUserInput;
        int userInput = 0; //have to initialize this variable since I will be using it in a block, setting it to 0 for now
        int checker =1;

        while(!in.hasNextInt()) { //Makes sure that user inputs an Integer, not String, double, etc
            System.out.println("Sorry, only numbers in integer form is allowed. Please enter your choice as an integer between 1 and 4");
            in.next();
        }
        do {
            
            do {
                try {
                    userInput = in.nextInt();
                        checker = 0;
                }
                catch (Exception e) {
                    System.out.println("Exception detectedddd");
                    in.nextLine(); // This line is to *clear the buffer* for Scanner
                }
            }while (checker ==1 );
    
            if ( userInput >= min && userInput <= max) {
                        System.out.println("you have chosen board " + userInput );
                        validUserInput = userInput;
                    }
            else {
                System.out.println(prompt);
                validUserInput = 0;
                
                 }  
        }while (validUserInput==0);
        
        return validUserInput;
    }
    
    // Main function
    public static void main (String args[]) {
        
        Scanner input = new Scanner(System.in);
        System.out.println("Choose a board style");
        
        readValidInt(input, "Bruh that's not valid", 1, 4);     
    }

}

这是我的输出(如您所见,当我输入“5”时,会打印出两件事 - “Exception detectedddd”和“Bruh that's not valid”,但后一句是 if else 语句的一部分,由于存在异常,因此不应达到:

选择棋盘样式

100

Bruh 那是无效的

五个

检测到异常ddd

Bruh 那是无效的

六个

检测到异常ddd

Bruh 那是无效的

10

Bruh 那是无效的

1

你选择了板 1

【问题讨论】:

    标签: java validation if-statement try-catch do-while


    【解决方案1】:

    在第一个输入 (100) 之后设置checker = 0;。由于值太大,您打印“Bruh that's not valid”并留在外循环中。

    这意味着您读取了另一个导致异常的值(五个)。但是,由于checker 已经为 0(从第一遍开始),因此不会重复内部循环。

    您需要在每次启动内部循环之前将checker 重置为 1:

        do {
    
            checker = 1;            
            do {
                try {
                    userInput = in.nextInt();
                    checker = 0;
                }
                catch (Exception e) {
                    System.out.println("Exception detectedddd");
                    in.nextLine(); // This line is to *clear the buffer* for Scanner
                }
            } while (checker == 1);
    
            //... remainder of the outer loop
        } while (validUserInput == 0);
    

    【讨论】:

    • 哇,谢谢。那是循环内循环内的一些初始循环。根本看不到那个错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多