java—在用户输入任何其他数据类型之后,我需要输入正确的输入

ifmq2ha2  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(376)

我试图用java制作一个菜单,在用户输入正确的输入之前获取用户输入,所以我使用 while() 在我的密码里。但当我运行代码时,唯一能运行的就是 while loop 它捕捉到错误,但它就停在那里。
以下是我目前的代码:

public static void main(String[] args) {

    // calling void choice
    choice();
    // System.out.println("");
    int choice = checkValidMenu();
    while (choice != 0) {
        // System.out.println("");
        choice = input.nextInt();
        if (choice == 1) {
            System.out.println("Enter 1 to validate an integer: ");
            choice = input.nextInt();
        } // choice 1
        else if (choice == 2) {
            System.out.println("Enter 2 to validate a decimal number: ");
            choice = input.nextInt();
        } // choice 2
        else if (choice == 3) {
            System.out.println("Enter 3 to process an array: ");
            choice = input.nextInt();
        } // choice 3
        else if (choice == 4) {
            System.out.println("Enter 4 to process a file: ");
            choice = input.nextInt();
        } // choice
        else if (choice == 5) {
            System.out.println("Enter 5 to exit:");
            choice = input.nextInt();
        } // choice5
    } // menu while

}// main

//method for choice
public static void choice() {
    System.out.println("\tMenu Options");
    System.out.println("Enter 1 to validate an integer: ");
    System.out.println("Enter 2 to validate a decimal choice: ");
    System.out.println("Enter 3 to process an array: ");
    System.out.println("Enter 4 to process a file");
    System.out.println("Enter 5 to exit: ");
}// void method */
    /// creating a method to validate user input

public static int checkValidMenu() {
    try {
        return input.nextInt();
    } catch (InputMismatchException e) {
        System.out.println("***Invalid entry - Try again");
        input.next();

        return 0;
    }
}

}// class
jvlzgdj9

jvlzgdj91#

在main方法中,对 choice=input.nextint() 在while循环线之后。不过,你可以添加更多的输入,让你的问题更清楚。

public static void main(String[] args) {

        // calling void choice
        choice();

        int choice = checkValidMenu();
        while (choice != 0) {

            //choice = input.nextInt();
            if (choice == 1) {
                System.out.println("Enter 1 to validate an integer: ");
                choice = input.nextInt();
            } 
            else if (choice == 2) {
                System.out.println("Enter 2 to validate a decimal number: ");
                choice = input.nextInt();
            } 
            else if (choice == 3) {
                System.out.println("Enter 3 to process an array: ");
                choice = input.nextInt();
            } 
            else if (choice == 4) {
                System.out.println("Enter 4 to process a file: ");
                choice = input.nextInt();
            } 
            else if (choice == 5) {
                System.out.println("Enter 5 to exit:");
                choice = input.nextInt();
            } 
        } 

    }
5cnsuln7

5cnsuln72#

您可以使用无限循环(例如。 while(true){} )显示菜单并在用户进入时中断此循环 5 作为选择。我还建议您在多个 if-else 声明。

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        boolean valid;
        while (true) {
            choice();
            int choice = Integer.parseInt(input.nextLine());
            if (choice == 5) {
                break;
            }
            switch (choice) {
            case 1:
                do {
                    valid = true;
                    try {
                        System.out.print("Enter an integer: ");
                        int x = Integer.parseInt(input.nextLine());
                        // ...process x here
                    } catch (NumberFormatException e) {
                        System.out.println("***Invalid entry - Try again");
                        valid = false;
                    }
                } while (!valid);
                break;

            case 2:
                do {
                    valid = true;
                    try {
                        System.out.print("Enter a number: ");
                        double x = Double.parseDouble(input.nextLine());
                        /// ...process x here
                    } catch (NumberFormatException e) {
                        System.out.println("***Invalid entry - Try again");
                        valid = false;
                    }
                } while (!valid);
                break;

            // ...other cases

            default:
                System.out.println("Invalid choice.");
            }
        }
    }

    public static void choice() {
        System.out.println("\tMenu Options");
        System.out.println("Enter 1 to validate an integer: ");
        System.out.println("Enter 2 to validate a decimal choice: ");
        System.out.println("Enter 3 to process an array: ");
        System.out.println("Enter 4 to process a file");
        System.out.println("Enter 5 to exit: ");
    }
}

另外,请注意,我使用了 Integer.parseInt(input.nextLine()) 而不是 input.nextInt() 为了避免这里讨论的问题。
示例运行:

Menu Options
Enter 1 to validate an integer: 
Enter 2 to validate a decimal choice: 
Enter 3 to process an array: 
Enter 4 to process a file
Enter 5 to exit: 
1
Enter an integer: abc

***Invalid entry - Try again

Enter an integer: 10.5

***Invalid entry - Try again

Enter an integer: 23
    Menu Options
Enter 1 to validate an integer: 
Enter 2 to validate a decimal choice: 
Enter 3 to process an array: 
Enter 4 to process a file
Enter 5 to exit:
wqlqzqxt

wqlqzqxt3#

捕捉到错误时,返回0,该值被分配给“choice”。因此,使用选项0进入while循环;在捕获无效条目时,可能不应该返回0?我不知道我是否明白你的意思。

相关问题