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 12 of 12

Thread: JAVA HIGH LOW NUMBER GUESSING GAME

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default JAVA HIGH LOW NUMBER GUESSING GAME

    Alright guys so I am writing this program so that the computer creates a number and the user has to guess it by just high and low as a hint. It also has to keep track of the number of tries and output them in a JOptionPane, actually everything has to be a JOP. I have an error on my while statement hopefully I have given you all enough info to figure this out.
     
    import java.math.*;
    import java.util.Random;
    import java.util.Scanner;
    import javax.swing.*;
     
    public class GuessingGame {
     
    	/**
    	 * @Alec Chamberlain
    	 * @Version 1.0
    	 * @Date:9/1/2011
    	 * Program plays a game where you have to guess the number 
    	 */
    	public static void main(String[] args){ 
     
    		// Variables
    		String guess;
    		int secretnum;
    		int counter = 0;
    		secretnum = (int) (Math.random()*1000+1);
    		int thereguess = Integer.parseInt(guess);
     
    		//Feel free to delete this. This is here so that you can have the number for testing
    	    System.out.println("Secret number is "+ secretnum);
     
    		//Loop in order to being checking input against 
    	do { guess = JOptionPane.showInputDialog(null, "Please enter your number to guess:");
    		counter++;
     
    		if (thereguess == secretnum){
    			counter++; 
    			JOptionPane.showMessageDialog(null, "Your guess is correct congrats it only took you:"+ counter+ " tries");
    			  break;
    		}
    		else if (thereguess < secretnum){
    			counter++; 
    			JOptionPane.showMessageDialog(null, "Your guess is to low try again"+ counter+ " tries");
    		}
    		else if (thereguess > secretnum){
    			counter++; 
    			JOptionPane.showMessageDialog(null, "Your guess is to high try again"+ counter+ " tries");
    		}
    		else if(thereguess< 0){
    			counter++; 
    			JOptionPane.showMessageDialog(null, "Good Job I Win-Thanks for playing. It took you:"+ counter+ " tries");
    			System.exit(0);
    		}
    	  } 
        while(guess != secretnum);
     
    	}	
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    What error are you getting in your while statement (if there's an exception being thrown, please post the exception message)?

    Also, what inputs/outputs are you using to generate that error?

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

    Laxman2809 (September 11th, 2011)

  4. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    Quote Originally Posted by Laxman2809 View Post
    I have an error on my while statement hopefully I have given you all enough info to figure this out.
    Hahahah. Good one! Although It's a simple problem, you didn't provide anything.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. The Following User Says Thank You to newbie For This Useful Post:

    Laxman2809 (September 11th, 2011)

  6. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    my fault its says incompatible operand types Sting and int. now i think i discovered the reason why and its because I'm trying to compare a string to an int value and you can't do that. but now i don't know how to fix that. obviously i have to change guess to an Int but then when I do that i get a lot of errors. now hopefully i have given enough

  7. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    int thereguess = Integer.parseInt(guess);
    You're parsing the string user guess outside of the loop. Just move this line inside of the do-while loop. Also, anywhere you're comparing vs. guess, compare vs. thereguess instead (thereguess is the integer value, guess is just the string representation of that value).

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    Laxman2809 (September 11th, 2011)

  9. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    Alright I moved that line in the do while statement and I still have the same error is there a chance that I have other issues with my code

  10. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    check my second comment about thereguess vs. guess and you're while loop conditional.

  11. The Following User Says Thank You to helloworld922 For This Useful Post:

    Laxman2809 (September 11th, 2011)

  12. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    I hate to ask for it but could i see an example of some pseudo code? I'm not fully grasping what your saying
     
    import java.math.*;
    import java.util.Random;
    import java.util.Scanner;
    import javax.swing.*;
     
    public class GuessingGame {
     
    	/**
    	 * @Alec Chamberlain
    	 * @Version 1.0
    	 * @Date:9/1/2011
    	 * Program plays a game where you have to guess the number 
    	 */
    	public static void main(String[] args){ 
     
    		// Variables
    		String guess;
    		int secretnum;
    		int counter = 0;
    		secretnum = (int) (Math.random()*1000+1);
     
    		//Feel free to delete this. This is here so that you can have the number for testing
    	    System.out.println("Secret number is "+ secretnum);
     
    		//Loop in order to being checking input against 
    	do { guess = JOptionPane.showInputDialog(null, "Please enter your number to guess:");
                    int thereguess = Integer.parseInt(guess);
    		counter++;
     
     
    		if (thereguess == secretnum){
    			counter++; 
    			JOptionPane.showMessageDialog(null, "Your guess is correct congrats it only took you:"+ counter+ " tries");
    			  break;
    		}
    		else if (thereguess < secretnum){
    			counter++; 
    			JOptionPane.showMessageDialog(null, "Your guess is to low try again"+ counter+ " tries");
    		}
    		else if (thereguess > secretnum){
    			counter++; 
    			JOptionPane.showMessageDialog(null, "Your guess is to high try again"+ counter+ " tries");
    		}
    		else if(thereguess< 0){
    			counter++; 
    			JOptionPane.showMessageDialog(null, "Good Job I Win-Thanks for playing. It took you:"+ counter+ " tries");
    			System.exit(0);
    		}
    	  } 
        while(guess != secretnum);
     
    	}	
    }
    Last edited by Laxman2809; September 11th, 2011 at 10:03 PM.

  13. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    compare these two lines of code very carefully (the first works, the second doesn't). Both were taken directly from your code.

    if (thereguess == secretnum)
    while(guess != secretnum);
    keep this in mind:

    Also, anywhere you're comparing vs. guess, compare vs. thereguess instead (thereguess is the integer value, guess is just the string representation of that value).

  14. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    Ok so now I have working code except when the user inputs a negative number I can't make the program quit.
     
    import java.math.*;
    import java.util.Random;
    import java.util.Scanner;
    import javax.swing.*;
     
    public class GuessingGame {
     
    	/**
    	 * @Alec Chamberlain
    	 * @Version 1.0
    	 * @Date:9/1/2011
    	 * Program plays a game where you have to guess the number 
    	 */
    	public static void main(String[] args){ 
     
    		// Variables
    		int guess;
    		int secretnum;
    		int counter = 0;
    		secretnum = (int) (Math.random()*1000+1);
     
    		//Feel free to delete this. This is here so that you can have the number for testing
    	    System.out.println("Secret number is "+ secretnum);
     
    		//Loop in order to being checking input against 
    	do { guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter your number to guess:"));
     
    		if (guess == secretnum){
    			counter = counter +1; 
    			JOptionPane.showMessageDialog(null, "Your guess is correct congrats it only took you: "+ counter+ " try");
    			  break;
    		}
    		else if (guess < secretnum){
    			counter = counter +1; 
    			JOptionPane.showMessageDialog(null, "Your guess is to low try again this is your "+ counter+ " try");
    		}
    		else if (guess > secretnum){
    			counter = counter +1; 
    			JOptionPane.showMessageDialog(null, "Your guess is to high try again this is your "+ counter+ " try");
    		}
    		else if(guess < 0){
    			counter = counter +1; 
    			JOptionPane.showMessageDialog(null, "Good Job I Win-Thanks for playing. It took you: "+ counter+ " try");
    			 break;
    		}
    	  } 
        while(guess != secretnum);
     
    	}	
    }

  15. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    What's it doing instead? I highly suggest stepping through this with a debugger, or at least in your head, to figure out the program flow. What happens when a user enters a negative number?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JAVA HIGH LOW NUMBER GUESSING GAME

    hint: check how you're chaining your conditionals. In an else-if statement, only ONE of the blocks gets executed (whichever one starting from the top happens to be true).

    // a simple example
    int i = 5;
    if(i == 5)
    {
        System.out.println("the first block was executed");
    }
    else if (i == 5)
    {
        System.out.println("the second block will not be executed if the first block is even if it's conditional check is true");
        // technically the conditional check is not even run, so the computer has no idea if it's true or not, but it doesn't matter
    }

Similar Threads

  1. Reverse Number guessing game
    By rarman555 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 23rd, 2011, 10:39 PM
  2. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 08:46 PM
  3. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 11:41 AM
  4. Guessing Game begginner question
    By okupa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 07:31 AM
  5. im in a hurry!!Help with a programm..java game of guessing a word
    By mr_doctor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:17 AM