Results 1 to 13 of 13
  1. #1
    CreatingDrake is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Code wont work have no clue why

    Hey guys so I have been in this java class for a while and am completely lost. We have to make a program that has the computer choose a number between 1 and 100 and then the user guesses the number until they get it right. Well half way through the program i run it and it gets a few steps in and then stops not doing the rest that I need it to do here Ill just post the code and let it do the talking.
    Java Code:
    public class NumberGuesser {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		//Intro
    		System.out.println("Welcome to the Number Guessing Game!");
    		System.out.println("I will think of a number between 1 and 100 and you will have to guess it. If you get it wrong I will tell you to aim higher or lower til you get it!");
    		System.out.println("Okay thinking of a number!");
    	
    		
    		try {
    		    Thread.sleep(2000);
    		} catch(InterruptedException ex) {
    		    Thread.currentThread().interrupt();
    		}
    		System.out.println("...");
    		try {
    		    Thread.sleep(2000);
    		} catch(InterruptedException ex) {
    		    Thread.currentThread().interrupt();
    		}
    		System.out.println("...");
    		try {
    		    Thread.sleep(2000);
    		} catch(InterruptedException ex) {
    		    Thread.currentThread().interrupt();
    		}
    		double RandomNumber = (double)(Math.random()*100+1);
    		System.out.println("Okay got it!");
    		//Guessing Phase
    		System.out.println("Okay go ahead and give it a guess!");
    		Scanner sc = new Scanner(System.in);
    		int UserGuess = sc.nextInt();
    		while (UserGuess != RandomNumber);{
    			if (UserGuess > RandomNumber);
    			System.out.println("Your guess is too high, try a bit lower");
    			if (UserGuess < RandomNumber);
    			System.out.println("Too low, go ahead and try a bit higher");
    			if (UserGuess == RandomNumber);
    			System.out.println("Good Job You Got It!");
    		
    		}
    	}
    
    }
    It has the user guess the number but nothing happens after can someone explain to me whats wrong with it.
    Last edited by CreatingDrake; 11-24-2013 at 10:00 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Code wont work have no clue why

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Can you copy the full contents of the console from when you execute the program and paste it here?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    CreatingDrake is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Re: Code wont work have no clue why

    Welcome to the Number Guessing Game!
    I will think of a number between 1 and 100 and you will have to guess it. If you get it wrong I will tell you to aim higher or lower til you get it!
    Okay thinking of a number!
    ...
    ...
    Okay got it!
    Okay go ahead and give it a guess!
    3

    __________

    Thats what happens the 3 was just a number i randomly decided on same thing happens no matter what number you pick, it just stops there.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Code wont work have no clue why

    Check for extra ; that end statements
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    CreatingDrake is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Re: Code wont work have no clue why

    I dont see any

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Code wont work have no clue why

    Look again. There should NOT be a ; between the ) after the condition and the { that starts the block of code to be controlled by the statement (while and if)
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    CreatingDrake is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Re: Code wont work have no clue why

    Thanks a ton! Now it repeats "Too low, go ahead and try a bit higher" infinitely how do I fix that I assume it has to do with "}"s or something along those lines

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Code wont work have no clue why

    infinitely
    That is because the variables in the condition are not changed. The code in the loop must change the value of a variable used in the loop to change the value (true or false) of the condition.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    CreatingDrake is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Re: Code wont work have no clue why

    By true or false I assume you mean booleans or something like that (barely touched on those in class) Ill look into that then.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Code wont work have no clue why

    Java Code:
    (UserGuess > RandomNumber)
    That is boolean expression that returns either true or false.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    CreatingDrake is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Re: Code wont work have no clue why

    Finished the code want to thank you a ton!

    Finished Code!:
    Java Code:
    public class NumberGuesser {
    	 
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //Intro
            System.out.println("Welcome to the Number Guessing Game!");
            System.out.println("I will think of a number between 1 and 100 and you will have to guess it. If you get it wrong I will tell you to aim higher or lower til you get it!");
            System.out.println("Okay thinking of a number!");
         
            // Just waits some time before printing "..." to give the code more character while "thinking"
            try {
                Thread.sleep(2000);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("...");
            try {
                Thread.sleep(2000);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("...");
            try {
                Thread.sleep(2000);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            int RandomNumber = (int)(Math.random()*100+1);
            System.out.println("Okay got it!");
            //Guessing Phase
            System.out.println("Okay go ahead and give it a guess!");
            Scanner sc = new Scanner(System.in);
            
            for (int x = 0; x < 500;){
            	int UserGuess = sc.nextInt();
                if (UserGuess > RandomNumber){
    	            x++;
    	            System.out.println("Guess Number: "+ x + " Your guess is too high, try a bit lower");
    	            }
                if (UserGuess < RandomNumber){
    	            x++;
    	            System.out.println("Guess Number: "+ x + " Too low, go ahead and try a bit higher");
    	            }
                if (UserGuess == RandomNumber){
    	            x++;
    	            System.out.println("Good Job You Got It! It only took you " + x + " tries" );}  
        }
      }
    }

  12. #12
    CreatingDrake is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Re: Code wont work have no clue why

    Yes the for loop is odd I just wanted to minimize effort and potential mistakes as much as possible plus it counts the amount of guesses quite easily.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Code wont work have no clue why

    Quote Originally Posted by CreatingDrake View Post
    Yes the for loop is odd I just wanted to minimize effort and potential mistakes as much as possible plus it counts the amount of guesses quite easily.
    Take a close look at your code again: whether the guess is too small, too large or correct, you increment 'x'; better do the increment in the for loop itself:

    Java Code:
    for (int x= 1; x <= 500; x++)
       ...
    Note that x starts at 1 because the remark 'it only took you 0 guesses' looks silly.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. netbeans project wont work on another pc ?
    By kavish in forum NetBeans
    Replies: 18
    Last Post: 08-05-2013, 11:10 AM
  2. Why wont it work???
    By fredrik8719 in forum New To Java
    Replies: 0
    Last Post: 04-08-2012, 07:38 PM
  3. Why Wont This Work???
    By Billywizz in forum New To Java
    Replies: 11
    Last Post: 03-09-2011, 02:33 AM
  4. why wont the %.2f work here?
    By jjth39347 in forum New To Java
    Replies: 2
    Last Post: 03-06-2011, 05:55 AM
  5. save will work but load wont?!?!
    By Sticks_ll in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •