切换案例验证

时间:2019-03-03 17:31:11

标签: java validation switch-statement

我正在尝试验证switch case语句,以确保用户不输入任何字符或字符串类型。我该如何实现?我一直在尝试使用while语句,但是无法验证它。

public static boolean showMenu()
{
    // Print welcome message
    System.out.println("\nApplication Menu : ");
    System.out.println("[1] Help");
    System.out.println("[2] Add");
    System.out.println("[3] Subtract");
    System.out.println("[4] Multiply");
    System.out.println("[5] Divide");
    System.out.println("[6] Quit");

    int SelectMenu = jin.nextInt();

    switch(SelectMenu)
    {

        case 1:
        showChoice();
        break;

        case 2:
        add();
        break;

        case 3:
        subtract();
        break;

        case 4:
        multiply();
        break;

        case 5:
        divide();
        break;

        case 6:
        return false;

    }
    return true;
}

3 个答案:

答案 0 :(得分:1)

您可以使用如下循环:

windows:
href="C:\users\documents\username\web\stylesheet.css"

答案 1 :(得分:0)

看看这个简单的例子,并使其适合您的代码:

/**
 *
 * @author Momir Sarac
 */
public class SwitchExample {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("\nApplication Menu : ");
        System.out.println("[1] Help");
        System.out.println("[2] Add");
        System.out.println("[3] Subtract");
        System.out.println("[4] Multiply");
        System.out.println("[5] Divide");
        System.out.println("[6] Quit");

        while (!scanner.hasNextInt()) scanner.next();
        int number = scanner.nextInt();
        switch (number) {
            case 1:
                clickedHelp();
                break;
            case 2:
                clickedAdd();
                break;
            case 3:
                clickedSubtract();
                break;
            case 4:
                clickedMultiply();
                break;
            case 5:
                clickedDivide();
                break;
            default:
                clickedSomething();

        }

    }

    public static void clickedHelp(){
        System.out.println("Help!");
    }


    public static void clickedAdd(){
        System.out.println("Add");
    }


    public static void clickedSubtract(){
        System.out.println("Subtract");
    }


    public static void clickedMultiply(){
        System.out.println("Multiply");
    }


    public static void clickedDivide(){
        System.out.println("Divide");
    }

    public static void clickedSomething(){
        System.out.println("Exit");
        System.exit(0);
    }

}

答案 2 :(得分:0)

希望您需要执行类似的操作

public static boolean showMenu()
{


    while(true) {

        // Print welcome message
        System.out.println("\nApplication Menu : ");
        System.out.println("[1] Help");
        System.out.println("[2] Add");
        System.out.println("[3] Subtract");
        System.out.println("[4] Multiply");
        System.out.println("[5] Divide");
        System.out.println("[6] Quit");

        int SelectMenu = jin.nextInt();

        switch(SelectMenu)
        {

            case 1:
                showChoice();
                break;

            case 2:
                add();
                break;

            case 3:
                subtract();
                break;

            case 4:
                multiply();
                break;

            case 5:
                divide();
                break;

            case 6:
                return false;

        }

    }

}