Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java,BufferedReader,限制用户输入,是/否_Java_If Statement_Bufferedreader_Nested If - Fatal编程技术网

Java,BufferedReader,限制用户输入,是/否

Java,BufferedReader,限制用户输入,是/否,java,if-statement,bufferedreader,nested-if,Java,If Statement,Bufferedreader,Nested If,我的问题是如何将用户输入限制为Y/N或Y/N(在Java中)。目前,我正在使用equals()并计划将其更改为equalsignorecase(),这应该可以处理案例部分。但是,这不会阻止用户输入其他字符(例如:H或H)。当前,当输入y或n以外的字符时,程序将直接进入“感谢您的播放消息”并结束游戏 我对编程比较陌生,所以请提供示例和建议,最好是完整的示例。这对我来说真的很重要。另外,如果您觉得这段代码可以用更好的方式编写,我愿意重写,但请再次提供完整的示例 我意识到这个问题对于stackover

我的问题是如何将用户输入限制为Y/N或Y/N(在Java中)。目前,我正在使用equals()并计划将其更改为equalsignorecase(),这应该可以处理案例部分。但是,这不会阻止用户输入其他字符(例如:H或H)。当前,当输入y或n以外的字符时,程序将直接进入“感谢您的播放消息”并结束游戏

我对编程比较陌生,所以请提供示例和建议,最好是完整的示例。这对我来说真的很重要。另外,如果您觉得这段代码可以用更好的方式编写,我愿意重写,但请再次提供完整的示例

我意识到这个问题对于stackoverflow来说有点宽泛,但我真的可以利用更有经验的程序的洞察力。谢谢你抽出时间

// creates instance of BufferedReader
// prompts user to play the game again
// places user input in a try
// if user wants to play again, call startGame()
// if user dosen't want to play again, keep asking anyways
private void showPlayAgainMessage() 
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println();
    System.out.println("Do you want to play again? (y/n)");

    try 
    {
        String playAgain = br.readLine();

        // Do you want to play again? Is y.
        if(playAgain.equals("y")) 
        {
            startGame();//else prompt another question with if else
        }

        // Do you want to play again? Is n.
        else if(playAgain.equals("n"))
        {
            System.out.println(); 
            System.out.println("Last chance.  Play again? (y/n)");
            playAgain = br.readLine(); 
                // Last chance.  Play again? Is y.
                if(playAgain.equals("y")) 
                {
                    startGame(); 
                }
                // Last chance.  Play again? Is n.
                else if(playAgain.equals("n")) 
                {
                    System.out.println(); 
                    System.out.println("How about Minesweeper? (y/n)");
                    playAgain = br.readLine();
                    // How about Minesweeper? Is y.
                        if(playAgain.equals("y")) 
                        {
                            System.out.println(); 
                            System.out.println("I really wish we had Minesweeper...");
                            System.out.println("Lots of Hangman though...Hangman? (y/n)");
                            playAgain = br.readLine();
                                // Lots of Hangman though...Hangman? Is y.
                                if(playAgain.equals("y")) 
                                {
                                    startGame(); 
                                }
                                // Lots of Hangman though...Hangman? Is n.
                                else if (playAgain.equals("n"))
                                {
                                    System.out.println();
                                    System.out.println("ok...");
                                }
                            }       
                        }
                    }
                }

这一串问题看起来很烦人,但如果必须的话,可以使用while循环和switch语句来实现状态机

int state = 0;
while (state < 4) {

    switch (state) {

    case 0: System.out.println("Do you want to play again? (y/n)"); break;
    case 1: System.out.println("Last chance.  Play again? (y/n)"); break;
    case 2: System.out.println("How about Minesweeper? (y/n)"); break;
    case 3: System.out.println("I really wish we had Minesweeper...");
            System.out.println("Lots of Hangman though...Hangman? (y/n)");break;
    }

    String playAgain = br.readLine();

    if(playAgain.equals("y")) 
    {
        startGame();
        state = 0;
    }
    else if(playAgain.equals("n")) {
        state++;
    }

}
System.out.println("ok...");
int state=0;
而(状态<4){
开关(状态){
案例0:System.out.println(“您想再次播放吗?(y/n)”);中断;
案例1:System.out.println(“最后一次机会。再次玩?(y/n)”;中断;
案例2:System.out.println(“扫雷舰怎么样?(y/n)”);中断;
案例3:System.out.println(“我真希望我们有扫雷舰…”);
System.out.println(“虽然有很多刽子手……刽子手?(y/n)”;break;
}
字符串playreach=br.readLine();
if(playreach.equals(“y”))
{
startGame();
状态=0;
}
else if(playreach.equals(“n”)){
状态++;
}
}
System.out.println(“ok…”);

每一个“n”答案都会让你进入下一个问题。非法输入将再次显示当前问题。

创建一个函数,检查用户输入,并随时调用它

public boolean checkInput(String in){

 if(in.equalsIgnoreCase("y") || in.equalsIgnoreCase("n"))
   return true;
 else
   return false;

}
从代码库中调用此方法,如

System.out.println();
System.out.println("Do you want to play again? (y/n)");

try 
{
    String playAgain = br.readLine();
    while(!checkInput){
        System.out.println("Please enter Valid Input Y/N");
        playAgain = br.readLine();
    }

    // Do you want to play again? Is y.
    if(playAgain.equals("y")) 
    {
        startGame();//else prompt another question with if else
    }

此处while循环中的代码不允许用户输入任何无效输入,如h、g等。它将提示用户输入无效,并应输入有效选项。

您只需简单地执行此操作,即为y/n以外的字符添加一个else部分。只需调用else部分中的showPlayAgainMessage()函数

您可以对代码的其他必需部分执行此操作

 private void showPlayAgainMessage() 
 {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println();
    System.out.println("Do you want to play again? (y/n)");

    try 
    {
        if (playAgain.equals("y")) {
            startGame();
        } 
        else if (playAgain.equals("n")) {
            System.out.println(); 
            System.out.println("Last chance.  Play again? (y/n)");
            playAgain = br.readLine(); 

           if (playAgain.equals("y")) {
                System.out.println();
                System.out.println("I really wish we had Minesweeper...");
                System.out.println("Lots of Hangman though...Hangman? (y/n)");
                playAgain = br.readLine();
                // Lots of Hangman though...Hangman? Is y.
                if (playAgain.equals("y")) {
                    startGame();
                } // Lots of Hangman though...Hangman? Is n.
                else if (playAgain.equals("n")) {
                    System.out.println();
                    System.out.println("ok...");
                }
            }

        }
        else{
            System.out.println("Sorry, invalid input. y/n required.");
            showPlayAgainMessage();   // this will prompt the user again for y/n if any other  character is typed.
        }
     }catch(Exception e){

     }
 }

这些都是好主意。我目前正在找人来审查我的程序,并提供一些反馈,这是一个刽子手游戏,5节课,没有什么疯狂。如果你能抽出一点时间,请发电子邮件给我reedwilliams8404@gmail.comthen无论您在哪里输入,都必须编写此else代码。所以这不是最好的办法