Scanner in Java does not work

I am trying to write a very simple guessing number (code below). After completing round 1, the user must decide whether he wants to play another round or not. The problem is that the program always skips the last question (never letting the user answer "y" or otherwise. What am I missing here? Is there something java.util.Scannerabout which I don't know?

import java.util.Random;
import java.util.Scanner;

public class GuessNum {

public GuessNum() {         

        int numRandom = 0;    
        int numGuess;    
        int life = 5;    
        String want = "";    
        Random rand = new Random();    
        Scanner scan = new Scanner(System.in);

        do {
            int lifeLeft = 5;
            numRandom = rand.nextInt(9)+1;

            System.out.print("\nGuess the Number [1..10]\n");
            System.out.print("===================\n");
            System.out.print("You have " + lifeLeft + " chances.\n");

            do {
                do {
                    System.out.print("What number do I have in mind: ");
                    numGuess = scan.nextInt();

                    if (numGuess < 1 || numGuess > 10)    
                        System.out.println("Invalid input. Range is 1-10.");    
                } while (numGuess < 1 || numGuess > 10);

                if (numGuess != numRandom && lifeLeft != 0)
                    System.out.println("Wrong! You only have " + --lifeLeft + " chances left.");

            } while (numGuess!=numRandom && lifeLeft > 0);

            if (numGuess == numRandom)
                System.out.println("Correct! -- in " + (life - lifeLeft) + " guess(es).");

            if (lifeLeft == 0) {
                System.out.println("You have no more lives..");
                System.out.println("This is the number: " + numRandom);
            }

            System.out.print("\nEnter 'y' if you want to play again or any other character to exit: ");
                want = scan.nextLine();
        } while (want.equals("y") || want.equals("Y"));
    }

    public static void main(String[] args) {            
        new GuessNum();
    }
}
+3
source share
2 answers

Use want = scan.next();instead nextLine().

The reason for your problem is that, following the previous one nextInt(), you are still on the same line, and nextLine()returns the rest of the current line.

:

Scanner sc = new Scanner(System.in);
System.out.println("nextInt() = " + sc.nextInt());
System.out.println("nextLine() = " + sc.nextLine());

, , 5, Enter, :

nextInt() = 5
nextLine() = 

nextLine() , .

, , 5 yeah!, Enter, :

nextInt() = 5
nextLine() =  yeah!

, " yeah!" , 5. :

String nextLine(): . , . .


, 1 10 , "":

numRandom = rand.nextInt(9)+1; // this can only be in 1..9 range inclusive!

java.util.Random:

int nextInt(int n): int 0 () ()

, Java API, Random.nextInt(int) .

+11

scan.next()+ scan.nextLine(); .

Scanner scan = new Scanner(System.in);

String s = scan.nextLine() +scan.nextLine();

- , - , nextLine() ( ). , , , nextLine()

0

Source: https://habr.com/ru/post/1754396/


All Articles