Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I print out a list of already guessed numbers?

    I'm having trouble figuring out how to print out a list of the numbers already guessed. I am able to print out the number that was just guessed but not previous guesses.

    Code:
       import java.util.*;
    
       public class GuessingGame extends Object
       {
          {
             return randomNumberGenerator.nextInt(16); 
          }
       
          public boolean PlayGuessingGame()
          {
             int randomNum =  GetRandomNumber();
             int guessNum;
             int [] numberOfGuesses;
             numberOfGuesses = new int[5];
             int runAgain;
          
             System.out.println("Welcome, new player!  Let's play the guessing game!");
             System.out.println("Rules of the guessing game:");	
             System.out.println("1. You've got 5 guesses to guess my secret number");
             System.out.println("2. The number is whole number that's between 0 and 15 (inclusive at both ends)");
             System.out.println("3. I'll give you hints about the number, if you don't guess right.");
             System.out.println("");
             System.out.println("Ok, I just thought of a new number for you to guess.");
             System.out.println("");	
             System.out.println("So far, you've guessed:");
             System.out.println("You have 5 guesses left");
             System.out.println("Your next guess?");
      
             guessNum = keyboard.nextInt();
          
     
             for(int i = 0; i < numberOfGuesses.length; i++)
             {
                if (guessNum == randomNum) //If guess correct
                {
                   System.out.println("Great guess!!! That's my number!!!");
                   System.out.println("Want to play again? (type 1 for 'Yes', and 0 for 'No')");
                   runAgain = keyboard.nextInt();
                   if(runAgain == 1)
                   {
                      return true; // Back to the Game_Program's main
                   }
                   else if (runAgain == 0)
                   {	
                      System.out.println("Have a nice day!");
                      return false;
                   }	
                			
                }	
                else if (guessNum > randomNum)
                {
                   System.out.println("I'm sorry, but my secret number is not " + guessNum);
                   System.out.println("My secret number is smaller than " + guessNum);
                
                }
                else if (guessNum < randomNum)
                
                { 
                   System.out.println("I'm sorry, but my secret number is not " + guessNum);
                   System.out.println("My secret number is greater than " + guessNum);
                }
                numberOfGuesses[i] = guessNum;
                System.out.println("So far, you've guessed: " + numberOfGuesses[i]);
                System.out.println("You have " + (4 - i) +  " guesses left");
                System.out.println("Your next guess?");
                guessNum = keyboard.nextInt();
             
             }
             System.out.println("You have no more guesses left");
             System.out.println("My secret number is: " + randomNum);
          
             return false;
          
          }
       }
    I didn't include the main since I have it broken down into multifiles. Any help would be greatly appreciated.

  • #2
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    Considering numberOfGuesses hold guesses by user, How do user guess numbers.

    One at a time: If so, Why are you looping numberOfGuesses against randomNumber. Are you adding guess to this array eveytime user guesses. Where are you printing all guesses?

    All at a time: When is this block used? To print all guesses?
    System.out.println("So far, you've guessed: " + numberOfGuesses[i]);
    System.out.println("You have " + (4 - i) + " guesses left");
    System.out.println("Your next guess?");

  • #3
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you spchinta for taking the time to read my post and most importantly replying to it.

    The following is the I/O result of the compiled program.

    Code:
    Welcome, new player!  Let's play the guessing game!
    Rules of the guessing game:
    1. You've got 5 guesses to guess my secret number
    2. The number is whole number that's between 0 and 15 (inclusive at both ends)
    3. I'll give you hints about the number, if you don't guess right.
    
    Ok, I just thought of a new number for you to guess.
    
    So far, you've guessed:
    You have 5 guesses left
    Your next guess?
    5
    I'm sorry, but my secret number is not 5
    My secret number is greater than 5
    So far, you've guessed: 5
    You have 4 guesses left
    Your next guess?
    10
    I'm sorry, but my secret number is not 10
    My secret number is smaller than 10
    So far, you've guessed: 10
    You have 3 guesses left
    Your next guess?
    Notice how in the line "So far, you've guessed: 10" it doesn't print out my previous guess which was 5. I apologize if you already knew this but I wanted to clarify just in case. I understand I need to use an array to accomplish this and thought

    for(int i = 0; i < numberOfGuesses.length; i++)

    is the code I needed to get it done.

  • #4
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    Is it possible to send entire code. You can attache the files, i guess, not sure of this. I think the way you are storing gusses in numOfGusses is wrong. Because if you add next guess 10 to array, it should iterate through 5 and 10 because of for loop. But the result i saw is only once..

  • #5
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    No worries..

    Remove the for loop and try this
    Code:
    int i = numberOfGuesses.length;
                numberOfGuesses[i] = guessNum;           
    			for(int i = 0; i < numberOfGuesses.length; i++)
    			{
    				System.out.print(" " + numberOfGuesses[i]);
    			 }
    instead of
    Code:
     numberOfGuesses[i] = guessNum;
                System.out.println("So far, you've guessed: " + numberOfGuesses[i]);

  • #6
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the first part of the file.

    Code:
    public class Game_Program extends Object
    {    
        public static void main(String[] args)
        {  
    		GuessingGame gg = new GuessingGame();
    		boolean playAgain = true;
    		while(playAgain)
    		{
    			playAgain = gg.PlayGuessingGame();
    		}
        }
    }

  • #7
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    Have you tried the code i gave you in last post. What is the result?

    Even with the piece of code you gave, There are still some parts missing.

  • #8
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry I was confused with the code that you suggested. You said to remove the for loop but with the code you wanted me to try it contains the same for loop. Also I can't use:

    int i = numberOfGuesses.length;

    because I already have int i defined in the for loop. I'm sorry, very new to java but I really do appreciate you helping me with this.

  • #9
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    Ok, Use this code for method PlayGuessingGame:
    Code:
    public boolean PlayGuessingGame(){
             int randomNum =  GetRandomNumber();
             int guessNum;
             int [] numberOfGuesses;
             numberOfGuesses = new int[5];
             int runAgain;
          
            System.out.println("Welcome, new player!  Let's play the guessing game!");
            System.out.println("Rules of the guessing game:");	
            System.out.println("1. You've got 5 guesses to guess my secret number");
            System.out.println("2. The number is whole number that's between 0 and 15 (inclusive at both ends)");
            System.out.println("3. I'll give you hints about the number, if you don't guess right.");
            System.out.println("");
            System.out.println("Ok, I just thought of a new number for you to guess.");
            System.out.println("");	
            System.out.println("So far, you've guessed:");
            System.out.println("You have 5 guesses left");
            System.out.println("Your next guess?");
      
            guessNum = keyboard.nextInt();      
    		for(int i = 0; i < numberOfGuesses.length; i++){
    			 
    			if (guessNum == randomNum){ //If guess correct            
    				   System.out.println("Great guess!!! That's my number!!!");
    				   System.out.println("Want to play again? (type 1 for 'Yes', and 0 for 'No')");
    				   runAgain = keyboard.nextInt();
    				   if(runAgain == 1)
    				   {
    					  return true; // Back to the Game_Program's main
    				   }
    				   else if (runAgain == 0)
    				   {	
    					  System.out.println("Have a nice day!");
    					  return false;
    				   }	
    							
    			}else if (guessNum > randomNum){
    				   System.out.println("I'm sorry, but my secret number is not " + guessNum);
    				   System.out.println("My secret number is smaller than " + guessNum);
    				
    			}else if (guessNum < randomNum){ 
    				   System.out.println("I'm sorry, but my secret number is not " + guessNum);
    				   System.out.println("My secret number is greater than " + guessNum);
    			}
    			
    			System.out.print("So far, you've guessed: " );
    			numberOfGuesses[i] = guessNum;           
    			for(int j = 0; j <= i; j++){
    				System.out.print(" " + numberOfGuesses[j]);
    			}
    			System.out.println();
                                        if(i==(numberOfGuesses.length-1)) break;
    			System.out.println("You have " + (4 - i) +  " guesses left");
    			System.out.println("Your next guess?");
    			guessNum = keyboard.nextInt();
             
    		}
            System.out.println("You have no more guesses left");
            System.out.println("My secret number is: " + randomNum);
          
            return false;
          
          }
    Let me know how that worked.
    Last edited by spchinta; 03-09-2011 at 06:16 PM.

  • #10
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm able to compile that code but experience a run-time error after guessing 1 number.

    Code:
    Welcome, new player!  Let's play the guessing game!
    Rules of the guessing game:
    1. You've got 5 guesses to guess my secret number
    2. The number is whole number that's between 0 and 15 (inclusive at both ends)
    3. I'll give you hints about the number, if you don't guess right.
    
    Ok, I just thought of a new number for you to guess.
    
    So far, you've guessed:
    You have 5 guesses left
    Your next guess?
    10
    I'm sorry, but my secret number is not 10
    My secret number is smaller than 10
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    	at GuessingGame.PlayGuessingGame(GuessingGame.java:67)
    	at Game_Program.main(Game_Program.java:16)

  • #11
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    This is the result for me.

    Code:
    Welcome, new player!  Let's play the guessing game!
    Rules of the guessing game:
    1. You've got 5 guesses to guess my secret number
    2. The number is whole number that's between 0 and 15 (inclusive at both ends)
    3. I'll give you hints about the number, if you don't guess right.
    
    Ok, I just thought of a new number for you to guess.
    
    So far, you've guessed:
    You have 5 guesses left
    Your next guess?
    5
    I'm sorry, but my secret number is not 5
    My secret number is greater than 5
    So far, you've guessed:  5
    You have 4 guesses left
    Your next guess?
    3
    I'm sorry, but my secret number is not 3
    My secret number is greater than 3
    So far, you've guessed:  5 3
    You have 3 guesses left
    Your next guess?
    10
    I'm sorry, but my secret number is not 10
    My secret number is greater than 10
    So far, you've guessed:  5 3 10
    You have 2 guesses left
    Your next guess?
    34
    I'm sorry, but my secret number is not 34
    My secret number is smaller than 34
    So far, you've guessed:  5 3 10 34
    You have 1 guesses left
    Your next guess?
    1
    I'm sorry, but my secret number is not 1
    My secret number is greater than 1
    So far, you've guessed:  5 3 10 34 1
    You have no more guesses left
    My secret number is: 15
    can you give me code for keyBoard and randomgenerator you used in GuessingGame.

    Also, there is no chance of getting run time error after "My secret number is smaller than 10" because we have one more system.out.println statement before doing anything.

    Can you please compare the code I posted with what you copied? Sorry to say this..

  • #12
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    IS this resolved or still having issues?


  •  

    Posting Permissions

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