-1

I've looked around and can't seem to find an answer as to what exactly the problem is. It executes fine up until the loop and then it seems to ignore the loop and gets hung up so I am rather confused.

package classGame;
import java.util.*;

public class GameTwo {
    static int randomNumber;
    static int numOfGuess = 5;
    static Scanner GameTwo = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("Frank: Hello there! My name is Frank. This is the introduction to the game.");
        System.out.print("Frank: Please tell me what you would like to be called: ");

        if(GameTwo.hasNextLine()) {

           String userName =GameTwo.nextLine();
           System.out.println(userName + ": My name is: " + userName);
           System.out.println("Frank: Well " + userName + ", it's nice to meet you. ");
           System.out.println("Frank: Lets play a little game, I want you to guess a number, It's already" +
        " in my head and it's between 1-10.");

           int guessResult = 1;
           int randomGuess = 0;
           while(guessResult != -1) {
             randomGuess = GameTwo.nextInt();
             guessResult = checkGuess(randomGuess);
           }        
           while (randomGuess != guessResult) {
             System.out.println(userName + ":Is the number: ");
             randomGuess = GameTwo.nextInt();

             if(randomGuess < 1 || randomGuess > 10 || randomGuess > guessResult || randomGuess < guessResult) {
                System.out.println("Frank: Thats not right "+ userName);
             } else if (randomGuess == guessResult) {
                System.out.println("Frank: Hey...Thats pretty good...You got it!");
            }
          }
        }
    }
    public static int getRandomNum () {
        randomNumber = (int) (Math.random()*10);
        return randomNumber;
    }

    public static int checkGuess(int guess) {
        if(guess == randomNumber) {
            return -1;
        } else {
            return guess;
        }
    }
}

here is what it prints out up to the loop

Frank: Hello there! My name is Frank. This is the introduction to the game.

Frank: Please tell me what you would like to be called: T

T: My name is: T

Frank: Well T, it's nice to meet you.

Frank: Lets play a little game, I want you to guess a number, It's already in my head and it's between 1-10.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
  • 2
    If you don't change the test variable, here `win`, *within* the while loop itself, how will this variable ever change? How then will the while loop ever exit? – Hovercraft Full Of Eels Feb 15 '17 at 19:48
  • Please read the [while loop tutorial](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html) section of the Basic Java Tutorials. – Hovercraft Full Of Eels Feb 15 '17 at 19:51
  • Wait; nevermind I realized I was stupid and I can use my original variables so I changed it to randomGuess != guessResult and left the last part as randomGuess == guessResult but my original problem persists, the program is not registering the loop at all. I completely removed the win variable. – Tristan Randolph Feb 15 '17 at 19:53
  • I would not name a variable the same way as the class. Try naming the `Scanner` variable with other name. – Lopan Feb 15 '17 at 19:58
  • Which loop does it ignore? – user253751 Feb 15 '17 at 19:58
  • while (randomGuess != guessResult) { System.out.println(userName + ":Is the number: "); randomGuess = GameTwo.nextInt(); if(randomGuess < 1 || randomGuess > 10 || randomGuess > guessResult || randomGuess < guessResult) { System.out.println("Frank: Thats not right "+ userName); } else if (randomGuess == guessResult) { System.out.println("Frank: Hey...Thats pretty good...You got it!"); } } – Tristan Randolph Feb 15 '17 at 19:59
  • Please, no code in comments where we are unable to read it. Instead edit,and improve your original question, showing the new code. – Hovercraft Full Of Eels Feb 15 '17 at 20:00
  • Well it "ignores" the loop that starts `while(randomGuess != guessResult)` because `randomGuess != guestResult` isn't true (duh) – user253751 Feb 15 '17 at 20:01
  • I am getting more confused by the moment. – Tristan Randolph Feb 15 '17 at 20:26

1 Answers1

0

I think this is an issue with how Scanner.nextInt() works. nextInt() takes in the next Integer found but it does not clear the buffer like nextLine() does.

See this link for more info about this issue: How does input.nextInt() work exactly?

Try this and see if your loop continues properly:

while(guessResult != -1) {

    randomGuess = GameTwo.nextInt();
    guessResult = checkGuess(randomGuess);
    GameTwo.nextLine();
    System.out.println("Random Guess: " + randomGuess); //Try here

} 

I think the reason the loop is getting hung up is because nextInt() keeps finding the randomGuess number still in the input buffer and executing over and over again. To test that, simply put System.out.println("Random Guess: " + randomGuess); in the loop and see if it is printing with the same number over and over again.

Otherwise, I would need to see the output of your program to further diagnose the issue.

Edit: Can you post the input/output of your program up to the point it crashes? This will help. Also, did you have the System.out.println() in your loop beginning with while(guessResult != -1) or the second one?

Edit 2: I tested this code with my edits and it seems to work as intended (ish). The initial while loop does not do what is intended. The "game" aspect of guessing the correct number all happens in the first loop. I am "playing" the game and guess numbers but once I get it correct, it moves to the second loop, presumably where you actually wanted to "play". The correct number is gotten in the first loop and then the guessResult variable gets set to -1. Then when the user tries to guess where they are supposed to, the "correct" number is now -1.

I don't think the game was ever "hung up", it was just silently waiting for your input in the first loop. To solve this:

Simply remove the first while loop (and its contents) and the game works as intended.

Community
  • 1
  • 1
themantimes8
  • 71
  • 10
  • Nothing; and the System.out.printlns I have in there aren't printing out already. – Tristan Randolph Feb 15 '17 at 20:26
  • Just edited the main post; and it freezes with what I originally had and with your edits – Tristan Randolph Feb 15 '17 at 20:37
  • I see what you mean I removed the first while loop and left the if, else if portion and then typed in random numbers and eventually I hit the correct one and the entire loop went through...you said it wasn't doing that for you though? – Tristan Randolph Feb 15 '17 at 21:07
  • @TristanRandolph It was doing this after removing the first while loop. What I was saying is that the first while-loop was preventing the second from executing until you guessed the correct number. After removing the first loop, the game works as I am guessing you intended? If so, please accept my answer. – themantimes8 Feb 15 '17 at 21:32