简体   繁体   中英

Java, BufferedReader, restrict user input, y/n

My question is how do I restrict user input to Y/N or y/n(in Java). Currently i'm using the equals() and plan to change them to equalsignorecase(), this should take care of the case part. However, this doesn't stop the user from entering other characters(ex: H or h). Currently when a character besides y or n is entered program proceeds straight to "thanks for playing message" and the end of the game.

I am relatively new to programming, so please provide examples with suggestion, preferably a complete example. It really goes a long way with me. Additional, if you feel this section of code could be written in a better way, I am open to rewrites, but once again please provide a full example.

I realize this question is a little broad for stackoverflow but I could really use the insight of more experienced programs. Thank you for your time.

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

This string of question looks really annoying, but if you must, you can use a while loop with a switch statement that implements a state machine.

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

Each "n" answer would advance you to the next question. Illegal input will show the current question again.

make a function that checks the user input and call it where ever you want.

public boolean checkInput(String in){

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

}

Call this method from your code base like

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
    }

Here the code in while loop will not allow the user to enter any invalid input like h,g, etc. It will prompt the user that the input is invalid and it should enter a valid option.

You need to simply do this ie, add an else part for characters other than y/n. Simply call the showPlayAgainMessage() function in the else part.

You may do this for other required sections of your code.

 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){

     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM