Java help (random number guessing game)

Discussion in 'Mac Programming' started by prostuff1, Apr 27, 2006.

  1. macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #1
    Thanks to everyone that helped with my last question...now i have another :0

    to know what the program needs to do go here.

    Here is the code i have so far (and it does basically all that it needs to do, but not all):

    Code:
    import java.util.Scanner;
    public class Lab4 
    {
    	public static void main(String[] args)
    	{
    		Scanner keyboard = new Scanner(System.in); //User input
    		int x = (int)(100 * Math.random()) + 1; //Generates random number
    		int userInt = 0; //user input number
    		char userAns1_1;
    		int counter = 0;
    		boolean hehe = true;
    		
    		System.out.print("Do you want to play a game: ");
    		
    		while (hehe == true)
    		{
    			String userAns1 = keyboard.nextLine();
    			userAns1_1 = userAns1.charAt(0); 
    			char userAns2 = 'y'; //Compare to Yes or No answer from user
    			
    			if (userAns1_1 != userAns2)
    			{
    				System.out.println("Goodbye!");
    				hehe = false;
    			}
    			else
    			{
    				System.out.println("\nI am thinking of a number between 1 and 100.  Try to guess it.\n");
    				
    				while (x != userInt)
    				{
    					System.out.print("What's your guess? ");
    					userInt = keyboard.nextInt();
    
    				if (x < userInt)
    				{
    					System.out.println(userInt + " is too big");
    				}
    				
    				else if (x > userInt)
    				{
    					System.out.println(userInt + " is too small");
    				}
    				counter = counter + 1;
    				}
    				if (counter == 1)
    				{
    					System.out.println("You've got it in " + counter + " guesses.  That was lucky!");
    				}
    				else if (counter >= 2 && counter <= 4)
    				{
    					System.out.println("You've got it in " + counter + " guesses.  That was amazing!");
    				}
    				else if (counter >= 5 && counter <= 6)
    				{
    					System.out.println("You've got it in " + counter + " guesses.  That was really good!");
    				}
    				else if (counter == 7)
    				{
    					System.out.println("You've got it in " + counter + " guesses.  That was ok!");
    				}
    				else if (counter >= 8 && counter <= 9)
    				{
    					System.out.println("You've got it in " + counter + " guesses.  That was pretty bad!");
    				}
    				else
    				{
    					System.out.println("You've got it in " + counter + " guesses.  This is not your game!");
    				}
    			}
    		}
    	}
    }
    I also get an error at the end of this when i end up getting the correct number.

    Here is the error message that i get:
    Thanks for any help you guys/gals can give me.
     
  2. macrumors 68000

    Joined:
    Jun 13, 2004
    Location:
    afk
    #2
    Just a suggestion,

    perhaps, you can try changing the way you do your first loop. instead of
    while (hehe) {
    //blah blah
    }

    do something like
    while ((userAns1=keyboard.nextLine())!=null) {
    //blah blah
    }
     
  3. macrumors 6502

    Joined:
    Nov 23, 2005
    #3
    You need to break out of that loop....

    Here is working code..

    Code:
    import java.util.Scanner;
    public class Lab4 
    {
    	public static void main(String[] args)
    	{
    		Scanner keyboard = new Scanner(System.in); //User input
    		int x = (int)(100 * Math.random()) + 1; //Generates random number
    		int userInt = 0; //user input number
    		char userAns1_1;
    		int counter = 0;
    		boolean hehe = true;
    		
    		System.out.print("Do you want to play a game: ");
    		String userAns1 = keyboard.nextLine();
    		userAns1_1 = userAns1.charAt(0); 
    		
    		while (hehe == true)
    		{
    			char userAns2 = 'y'; //Compare to Yes or No answer from user
    			
    			if (userAns1_1 != userAns2)
    			{
    				System.out.println("Goodbye!");
    				hehe = false;
    			}
    			else
    			{
    				System.out.println("\nI am thinking of a number between 1 and 100.  Try to guess it.\n");
    				
    				while (x != userInt)
    				{
    					System.out.print("What's your guess? ");
    					userInt = keyboard.nextInt();
    
    				if (x < userInt)
    				{
    					System.out.println(userInt + " is too big");
    				}
    				
    				else if (x > userInt)
    				{
    					System.out.println(userInt + " is too small");
    				}
    				counter = counter + 1;
    				}
    				if (counter == 1)
    				{
    					System.out.println("You've got it in " + counter + " guesses.  That was lucky!");
    					break;
    				}
    				else if (counter >= 2 && counter <= 4)
    				{
    					System.out.println("You've got it in " + counter + " guesses.  That was amazing!");
    					break;
    				}
    				else if (counter >= 5 && counter <= 6)
    				{
    					System.out.println("You've got it in " + counter + " guesses.  That was really good!");
    					break;
    				}
    				else if (counter == 7)
    				{
    					System.out.println("You've got it in " + counter + " guesses.  That was ok!");
    					break;
    				}
    				else if (counter >= 8 && counter <= 9)
    				{
    					System.out.println("You've got it in " + counter + " guesses.  That was pretty bad!");
    					break;
    				}
    				else
    				{
    					System.out.println("You've got it in " + counter + " guesses.  This is not your game!");
    					break;
    				}
    			}
    		}		
    	}
    }
     
  4. Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #4
    jtalerico: If you use the CODE tag instead of the QUOTE tag then the spacing of your code will be preserved when you post it.

    I fixed it for you here, but remember that tip for the next time.
     
  5. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #5
    Beat me to it. :)
     
  6. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #6
    That huge if block is a waste too. Instead try something like this:

    String comments[] = new String[5];
    comments[0] = "You're terrible!";
    comments[1] = "You're pretty bad!";
    etc.
    comments[4] = "You're amazing!";

    Then to use it:

    System.out.println("It took you " + counter + " tries to get the right answer. " + comments[counter/2]; //intentional int divide

    Notice that you should ensure counter < comments * 2.
     
  7. thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #7
    This works great and thanks for the help but unfortunantly i need that whole process to loop and ask the user if they want to play another game.

    So the whole process starts and when the user guesses the correct number it outputs the number of guesses and give the little message. After that print out it should ask the user "Do you want to play another game?" and then if the user enters yes the whole process starts over with a new variable and everything. Until the user enters "no" that process will not stop. I can't seem to figure out how to make it keep looping.

    Any help would be appreciated.

    That is fine advice but we have not learned that kind of stuff in class yet and i think the teacher would wonder about it.

    Any help would be greatly appreciated.

    Thanks
     
  8. macrumors 6502

    Joined:
    Nov 23, 2005
    #8
    you can make it a method such as

    Code:
    public void playgame(){
    your code 
    }
    Then after the user wins or whatever, just call the method again, this.playgame();
     
  9. macrumors 6502

    Joined:
    Nov 23, 2005
    #9
    The other comment is using an array, it is not that hard! check the API
     
  10. thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #10
    thanks for the help. I figured it out but i can tell you that is probably not the prettiest thing ever invented. but it does work and get the job done.

    Thansk for the help!!
     

Share This Page