1

I'm new to java programming and was testing some of the stuff I learned in order to make a little guessing game. It works and you can run through it, but after you get the second number wrong, you aren't prompted telling you whether the number is lower or higher. Here is an example of the problem:

Guess a number, 1 through 100: 
50
Guess higher! 
75
75
Guess lower! 
65
65
Guess lower! 

And here is the code:

public static void main(String[] args) {
    Random num = new Random();
    Scanner scan = new Scanner(System.in);
    int rand;
    boolean test;
    int rand2;
    int guess = 0;

    rand = num.nextInt(100);
    System.out.println("Guess a number, 1 through 100: ");
    while(test = true){
        rand2 = scan.nextInt();
        if(rand == rand2){
            guess++;
            if(guess < 19){
                System.out.println("Thats the correct number! And it only took: " + guess + " tries");
            }else{
                System.out.println("It took you: " + guess + " tries to guess the number!");
            }

        }else if(rand < rand2){
            System.out.println("Guess lower! ");
            guess++;
            rand2 = scan.nextInt();
        }else if(rand > rand2){
            System.out.println("Guess higher! ");
            guess++;
            rand2 = scan.nextInt();
        }
    }
}
CrazyJavaLearner
  • 368
  • 8
  • 17
NYBoss00
  • 27
  • 2
  • 1
    First problem I see, you read the next int in the two condition and again in the beginning of the loop (so the first one after the print Guess lower/higher is ignore). – AxelH Aug 03 '16 at 06:11
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Aug 03 '16 at 07:16

3 Answers3

0

Remove rand2 = scan.nextInt(); in both else if block and try running it. Your logic is like taking two times input from user until you get correct answer.

Suseendran P
  • 557
  • 1
  • 5
  • 18
0

You are scanning for another number twice before you do the next check for lower or higher. Once is in the if else statement and the other is at the top of the while loop. Try removing the scan.nextInt() static method calls inside the if/else statement and it should work like you want.

while(test = true){
        rand2 = scan.nextInt();
        guess++;
        if(rand == rand2){

            if(guess < 19){
                System.out.println("Thats the correct number! And it only took: " + guess + " tries");
                break;
            }else{
                System.out.println("It took you: " + guess + " tries to guess the number!");
            }
        }else if(rand < rand2){
            System.out.println("Guess lower! ");
        }else if(rand > rand2){
            System.out.println("Guess higher! ");
        }
    }
ghg565
  • 422
  • 6
  • 15
0

I have correct it for you, see following:

public static void main(String[] args) {

        Random num = new Random();
        Scanner scan = new Scanner(System.in);
        boolean isOK = false;
        int counter = 0;

        int randNum = num.nextInt(100);
        while(true) {
            counter++;

            int n = scan.nextInt();
            if(n == randNum) {
                System.out.println("OK in " + counter + " times");
                isOK = true;
            } else if(n > randNum) {
                System.out.println("Lower");
            } else {
                System.out.println("Higher");
            }
        }
    }
xxx
  • 3,315
  • 5
  • 21
  • 40