# Problem Script: Random Number Guessing Game

• July 7th, 2013, 08:11 PM
superjacko
Problem Script: Random Number Guessing Game
Hello Community there is a problem in this script.

The purpose of this script is a Random Number Guess Game. The program runs fines but once the guess is correct the loop doesn't exit.

Code :

```public static void main(String[] args){ // Declaring Objects Scanner input = new Scanner(System.in); Random rand = new Random();   //Declaring Variables int userGuess; int computerGuess; boolean gameFinish = false;   //Setting up random number generator computerGuess = 1+rand.nextInt(40); System.out.println(computerGuess);   //the player may enter a number between 1 and 40 while(gameFinish = true){ System.out.println("Enter a number between 1 to 40"); userGuess = input.nextInt();   //The three IFs shows if the player is higher or lower from the computer's generated number if (userGuess == computerGuess){ System.out.println("Good Job"); gameFinish = true; } if(userGuess < computerGuess){ System.out.println("Higher"); gameFinish = false; } if(userGuess > computerGuess){ System.out.println("Lower"); gameFinish = false; } } } }```
• July 7th, 2013, 08:33 PM
pbrockway2
Re: Problem Script: Random Number Guessing Game
Quote:

Code :

`while(gameFinish = true){`

In that while loop you assign true to gameFinish instead of comparing it using one of the boolean operators == and !=.

Notice that the while loop should keep going while gameFinish is false, not true. The usual way of writing that is

Code :

`while(!gameFinish){`

This is equivalent to "while(gameFinish==false)" but easier to read.

---

When you post code, use "code" tags. Put [code] at the start of a section of code and [/code] at the end and the forum software will format the code properly. You should avoid any other formatting (bold, colours etc) within code. Also spaces are much more reliable than tabs for indenting when the code is destined to be displayed on a web browser.
• July 7th, 2013, 08:53 PM
superjacko
Re: Problem Script: Random Number Guessing Game
thanks you very much for solving my problem. Thank you for the tips on setting out my code :o and also I will have to get used to these contractions "while(!gameFinish)".
• July 7th, 2013, 09:18 PM
pbrockway2
Re: Problem Script: Random Number Guessing Game
You're welcome.
• July 7th, 2013, 09:57 PM
pbj
Re: Problem Script: Random Number Guessing Game
o