0

I am creating a simple game where a random number is generated and if you guess right you win! My problem: There is a while loop (run) to keep running the game until a player decides to quit. When the player selects quit, the loop is supposed to be set to false and stop, but when a player hits quit, the program repeats the loop. The part of the program where the boolean is set to false is bolded.

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

public class RunHiLowGame {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //variables
        String name;
        int guess;
        int guessnum = 0;
        boolean run = true;
        int number;
        int choice;

        Scanner input = new Scanner(System.in);
        Random r = new Random();

        number = 1;

        //welcome


        //create loop to ask guess then check if high or low        
        while (run = true) {
            //enter guess
            System.out.println("Enter your guess:");
            guess = input.nextInt();

            //if low say press 1 to retry
            if (guess > 0 && guess < 100) {
                if (guess < number) {
                    System.out.println("Your number was too low! Guess again!");
                    guessnum++; 
                }
                else if (guess > number) {
                    System.out.println("Your number was too high! Guess again!");   
                    guessnum++;
                }
                else if (guess == number) {
                    System.out.println("You have won!");       
                    guessnum++;
                    System.out.println("Amount of guesses:" + guessnum );
                    System.out.println("Press 1 to play again or press 2 to exit     the game.");
                    choice = input.nextInt();
                    if (choice == 1) {
                        System.out.println("Restarting.\n");
                    }
                    else if (choice == 2) {
                        System.out.println("Exiting.");
                        run = false
                    }               
                    else {
                        System.out.println("Error.");
                    }
                }
                else {
                    System.out.println("Invalid answer.");
                }
             }
         }
         System.out.println("Exited.");
     }
}
1
  • It appears the part has not been bolded because it is formatted to code. My apologies. Commented Sep 2, 2015 at 1:22

3 Answers 3

6

You have a single = when you need two here

while(run = true)

with one, it's assignment (which also yields the value assigned in Java). Something like,

while(run == true)

you can also use the simpler

while (run)
Sign up to request clarification or add additional context in comments.

2 Comments

Also, Correct me if I'm wrong here, but the user must guess the correct number before they can exit the game judging by the code and layout of the else if statements? hence making exiting not possible until the game has been won
Just saw that this question has 14k views, not exactly sure why. Glad we have chatgpt for these types of errors now. Thanks.
0

The operator for checking equality is ==. Your expression for the while loop, run = true, assigns true to run.

Comments

0

I think you should replace "run = false" with "System.exit(0);".

I think the loop is self-contradictory because it works only if run is true. Once run is false, the loop doesn't work and run therefore isn't false. To test this, write this code after pressing quit: System.out.println (run ? "Run is true" : "Run is false").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.