【问题标题】:In Java, how to restrict a user to enter only one type?在Java中,如何限制用户只能输入一种类型?
【发布时间】:2021-03-15 00:37:15
【问题描述】:

在这种情况下,我要求用户输入 1,2 或 3。如果用户输入字符串,它应该再次提问。当我的代码运行时,每个 if 语句都在运行,而我想运行一个特定的 if 语句。如果有人可以帮助我,那将是一个巨大的帮助。

    String question = "What is the color of banana? Write 1,2 or 3. Choose one of the following:";
    String answerOne = "1. yellow";
    String answerTwo = "2. red";
    String answerThree = "3. blue";

    while(true){
        System.out.println(question + "\n" + answerOne + "\n" + answerTwo + "\n" + answerThree);
        Scanner input = new Scanner(System.in);
        if (input.hasNext()) {
            System.out.println("Please select a NUMBER between 1 and 3.");
        }
        if (input.hasNextDouble()) {
            System.out.println("Please select a NUMBER between 1 and 3.");
        }
        if (input.hasNextInt()) {
            int x = input.nextInt();
            if (x == 1) {
                System.out.println("Congratulations!! your answer is correct");
                break;
            }
            if (x == 2 || x == 3) {
                System.out.println("Sorry!! the correct answer is" + answerOne);
                break;
            }
            if (x > 3) {
                System.out.println("Please choose the correct answer!!");
            }
        }

    }
}

【问题讨论】:

    标签: java if-statement input while-loop integer


    【解决方案1】:

    好的,首先.hasSomething() 不是这样工作的,它用于检查是否有更多元素。因此,您应该做的是将输入读取为字符串并检查该字符串:

     String question = "What is the color of banana? Write 1,2 or 3. Choose one of the following:";
            String answerOne = "1. yellow";
            String answerTwo = "2. red";
            String answerThree = "3. blue";
    
                System.out.println(question + "\n" + answerOne + "\n" + answerTwo + "\n" + answerThree);
                Scanner input = new Scanner(System.in);
                
                String c = input.next();//read the input
                
                while(!(c.charAt(0) >= '1' && c.charAt(0) <= '3') || c.length() != 1){ //check if it's a number between 1 and 3
                    System.out.println("Enter agin:");
                    c = input.next();
                }
                
    
                int x = Integer.parseInt(c);//change c to an integer
                if (x == 1) {
                    System.out.println("Congratulations!! your answer is correct");
                    break;
                }
                if (x == 2 || x == 3) {
                    System.out.println("Sorry!! the correct answer is" + answerOne);
                }
    

    【讨论】:

    • 您好,请查看此 qn stackoverflow.com/q/60680525/4860775。我不明白这个解决方案是如何工作的stackoverflow.com/a/63753644/4860775
    • @ASharma7 你读过评论了吗?“这是一个错误的解决方案。这假设字符串的子字符串不能反转。请参阅下面的答案 stackoverflow.com/a/63753644/ 3904327 - 阿舒托什查莫利“
    • 我更喜欢这种方法,但我认为您的循环比绝对必要的要多。如果输入无效,您可以继续外循环。不过可能是口味问题。
    • @lierwu 是的,我也指的是 ashutosh chamoli 的相同解决方案。我想我在复制 Ashutosh 的网址时犯了一些错误
    • @ASharma7 问他,我也不知道... QAQ :(
    【解决方案2】:

    我认为你的问题如下。

    1. 您每次在循环中创建一个新的扫描仪

    2. 在新扫描仪创建后,您立即执行几个“hasNext”测试。什么都不会被输入。你可能会永远在那个循环中旋转。

    3. 整数也是双精度数,因此即使用户输入了整数,它也会满足“if double”条件。

    4. 如果未键入整数,则永远不会读取输入,因此您会卡在输入流中的那个点。你会永远循环下去。

    流程应该是这样的:

    input = new Scanner();
    while (true) {
        print question...
    
        try {
            int x = input.nextInt(); // waits for user input
            process as before;
            if (correct answer) {
               break out of loop;
            }
            else {
               print "nope";
            }
         }
         catch (InputMismatchException ex) { // something that is not an integer
              print "type an integer";
              input.nextLine(); // skip incorrect input
         }
    }
    

    当然,这只是一个伪代码草图。

    它使用异常处理作为确保输入整数的最便捷方式。通过在扫描仪上调用 nextInt(),我们假设用户将键入可转换为整数的内容。 try/catch 结构处理错误情况。

    还有其他的写法;我自己的倾向是阅读一行并解析它。但这最接近我认为的初衷。

    【讨论】:

    • 什么是input.hasNextInt()?你的意思是checkInputIsInteger?_?
    • 我的意思是 Scanner 类的 hasNextInt() 方法,与 OP 在其原始代码中使用的完全一样。虽然我现在看到我的回答重复了 OP 在不等待输入方面的缺陷。我会解决的。
    • 但是您不允许扫描仪读取任何输入。它不会等待......所以它会一直返回 false QAQ
    • 是的,你说得对。会修。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 2017-06-03
    相关资源
    最近更新 更多