Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem Script: Random Number Guessing Game

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Talking 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.

    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;
    				}
    			}
    			}	
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Problem Script: Random Number Guessing Game

    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

    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.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    superjacko (July 7th, 2013)

  4. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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 and also I will have to get used to these contractions "while(!gameFinish)".

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Problem Script: Random Number Guessing Game

    You're welcome.

  6. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem Script: Random Number Guessing Game

    o

Similar Threads

  1. matching game random number problem
    By nubshat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 08:58 AM
  2. Basic Number guessing game.
    By Ddng940 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2012, 08:01 PM
  3. Reverse Number guessing game
    By rarman555 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 23rd, 2011, 10:39 PM
  4. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 09:46 PM
  5. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 12:41 PM