繁体   English   中英

在 Java 中,如何限制用户只能输入一种类型?

[英]In Java, how to restrict a user to enter only one type?

在这种情况下,我要求用户输入 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!!");
            }
        }

    }
}

好的,首先这不是.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);
            }

我认为你的问题如下。

  1. 每次围绕循环创建一个新的扫描仪

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

  3. integer 也是双精度数,因此即使用户键入了 integer,它也会满足“如果双精度数”条件。

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

流程应如下所示:

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM