首页 > 解决方案 > In Java, how to restrict a user to enter only one type?

问题描述

In this case, I have asked the user to enter 1,2 or 3. If user enters string, it should ask the question again. when my code runs, every if statement is running, where as I want to run a specific if statement. It would be a huge favor if anyone could help me.

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

    }
}

标签: javaif-statementinputwhile-loopinteger

解决方案


Ok, so first that's not how .hasSomething() works, it used to check if there have more elements. So what you should do, is to read the input as a string and check that string:

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

推荐阅读