0

我的问题是如何将用户输入限制为 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...");
                                }
                            }       
                        }
                    }
                }
4

3 回答 3

0

您需要简单地执行此操作,即为 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){

     }
 }
于 2014-11-07T05:27:06.723 回答
0

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

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...");

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

于 2014-11-07T05:21:36.823 回答
0

创建一个检查用户输入的函数,并在任何你想要的地方调用它。

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等。它会提示用户输入无效,它应该输入一个有效的选项。

于 2014-11-07T05:23:17.740 回答