简体   繁体   中英

Line of code in while loop executing at wrong time

Program code:

public static void main(String[] args) throws IOException {

    System.out.print("Welcome to my guessing game! "
            + "Would you like to play (Y/N)? ");
    yesOrNoAnswer = (char)System.in.read();

    if(yesOrNoAnswer == 'Y') {
        System.out.print("\n\nGuess the number (between 1 and 10): ");
        while(AnswerIsCorrect == false) {
            guess = System.in.read();

            if(guess == correctAnswer) {
                AnswerIsCorrect = true;
            }
            else {
                System.out.print("\nYou guessed wrong! Please try again: ");
            }
        }
        System.out.print("You guessed correct! Congratulations!"
                + "\n\nPress any key to exit the program . . .");
        System.in.read();
    }
}

Expected output:

Welcome to my guessing game! Would you like to play (Y/N)? Y


Guess the number (between 1 and 10): 

Actual output:

Welcome to my guessing game! Would you like to play (Y/N)? Y


Guess the number (between 1 and 10): 
You guessed wrong! Please try again:

When I input 'Y' at the first question (would you like to play) it proceeds to output, "Guess the number between 1 and 10: " This is a good output. However, before I can enter a number, it instantly outputs, "You guessed wrong! Please try again: "

How can I fix this code to achieve the expected output?

The problem lies at your usage of System.in.read() .

System.in.read() will read characters one by one and return them as int s. If I enter 1 , System.in.read() will return 49 because that's what the character 1 is encoded to.

The reason why it immediately prints that your guess is wrong without letting you type anything is that System.in.read() reads new line characters as well. If there is anything unread, it will read that, instead of asking for new input. There is a new line character after the Y you entered, so it reads that new line character.

You should instead use a Scanner :

    Scanner scanner = new Scanner(System.in); // create a new scanner
    System.out.print("Welcome to my guessing game! "
        + "Would you like to play (Y/N)? ");
yesOrNoAnswer = scanner.nextLine().charAt(0); // reading the first character from the next line

if(yesOrNoAnswer == 'Y') {
    System.out.print("\n\nGuess the number (between 1 and 10): ");
    while(AnswerIsCorrect == false) {
        guess = Integer.parseInt(scanner.nextLine()); // get an int from the next line

        if(guess == correctAnswer) {
            AnswerIsCorrect = true;
        }
        else {
            System.out.print("\nYou guessed wrong! Please try again: ");
        }
    }
    System.out.print("You guessed correct! Congratulations!"
            + "\n\nPress any key to exit the program . . .");
    scanner.nextLine();
}

Scanner.nextLine() will return the input the user typed as a string and it ignores new line characters.

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