📜  验证字符串输入 java 代码示例

📅  最后修改于: 2022-03-11 14:52:36.655000             🧑  作者: Mango

代码示例1
boolean quit = false;
do{ // change your while by this

    // your own stuff here


    // then after all your program stuff
    boolean choiceIsOK = false;
    do{
    String userinput = sc.next();
    char choice = userinput.toLowerCase().charAt(0);
    switch(choice){
    case 'y':
        // case y, do nothing, you could even remove that case.
        choiceIsOK = true;
        break;
    case 'n':
        // case n, do something here
        choiceIsOK = false;
        quit = true;
        break;
    default:
        // error or warning
        System.out.println("Type Y or N to respectively continue or quit");
        break;
    }
    }while(!choiceIsOK);
}while (!quit);