【问题标题】:Why is my validation function not working (to validate if user input is an integer within range)为什么我的验证功能不起作用(验证用户输入是否为范围内的整数)
【发布时间】:2021-09-01 09:39:33
【问题描述】:

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

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);
}

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

【问题讨论】:

  • do { ... }while (checker==0); this 从不循环,每次第一次迭代后都会返回。 if 的两条路径都返回一个值,因此它永远不会进入第二次迭代。
  • 当您告诉您的程序“返回”时,它会执行此操作,无论该代码周围是否有循环。
  • 谢谢@Michael
  • 谢谢你@Tom
  • 不敢相信我学了 8 个月的 Java 却没有意识到这一点

标签: java if-statement while-loop java.util.scanner do-while


【解决方案1】:

问题出在您的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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2011-07-20
    • 2019-04-27
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多