为什么我的验证函数不起作用(验证用户输入是否是范围内的整数)

Why is my validation function not working (to validate if user input is an integer within range)

我正在尝试创建一个验证函数,但当我试图通过将所有内容放入函数中来使代码“更简洁”时,代码停止工作。 这是我的原始代码:

static int readValidInt(Scanner in, String prompt, int min,  int max){
    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();
    }
    int a = in.nextInt();
    if ( a >= min && a <= max) {
        System.out.println("you have chosen board"+ a );
        return a;   
    }
    else {
        System.out.println(prompt);
        return 0;
    }
    //in main, use a do while loop to keep this running until the input is right(until a becomes something that is not 0)
}

public static void main (String args[]) {
    int validinput;
    Scanner input = new Scanner(System.in);
    System.out.println("WELCOME TO CS300 PEG SOLITAIR GAME !" + "\n====================================");
    System.out.println("Board Style Menu");
    System.out.println("\t1) Cross \n \t2) Circle \n \t3) Triangle \n\t3) Simple T"); //Prints out the 4 options of boards, spacing all of them with \t and \n
    System.out.println("Choose a board style");
    do {
        validinput = readValidInt(input, "Bruh that's not valid", 1, 4);
    }
    while (validinput == 0);
}

这是输出:

这是我试图从 main 中取出 do while 循环的版本的代码:

public static int readValidInt(Scanner in, String prompt, int min,  int max){   
    int checker;
    int a;

    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 {
         a = in.nextInt();
            if ( a >= min && a <= max) {
                    System.out.println("you have chosen board"+ a );
                    checker = 1;
                    return a;   

                }
        else {
            System.out.println(prompt);
            checker = 0;
            return 0;
        }   
    }while (checker==0);
}

public static void main (String args[]) {
    
    Scanner input = new Scanner(System.in);
    System.out.println("WELCOME TO CS300 PEG SOLITssssAIR GAME !" + "\n====================================");
    System.out.println("Board Style Menu");
    System.out.println("\t1) Cross \n \t2) Circle \n \t3) Triangle \n\t4) Simple T"); //Prints out the 4 options of boards, spacing all of them with \t and \n
    System.out.println("Choose a board style");
    
    readValidInt(input, "Bruh that's not valid", 1, 4);
}

这是不起作用的输出(它在没有给我再次尝试的情况下终止):

问题出在你的 if-else 分支,你总是 return (这将退出方法)在第一个循环迭代中无论如何。你永远不会循环。

要解决此问题,您需要将 checker 的值设置为您尝试 return 之前的值,然后 return checker 之后 循环:

public static int readValidInt(Scanner in, String prompt, int min, int max) {
    int checker;
    int a;

    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 {
        a = in.nextInt();
        if (a >= min && a <= max) {
            System.out.println("you have chosen board" + a);
            checker = a;

        } else {
            System.out.println(prompt);
            checker = 0;
        }
    } while (checker == 0);
    
    return checker;
}

输出:

Choose a board style
five
Sorry, only numbers in integer form is allowed. Please enter your choice as an integer between 1 and 4
5
Bruh that's not valid
1
you have chosen board1