Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-06-2011, 06:54 PM   PM User | #1
SYNACK
New to the CF scene

 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
SYNACK is an unknown quantity at this point
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.
SYNACK is offline   Reply With Quote
Old 03-08-2011, 12:02 AM   PM User | #2
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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?");
spchinta is offline   Reply With Quote
Old 03-08-2011, 12:20 AM   PM User | #3
SYNACK
New to the CF scene

 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
SYNACK is an unknown quantity at this point
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.
SYNACK is offline   Reply With Quote
Old 03-08-2011, 12:36 AM   PM User | #4
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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..
spchinta is offline   Reply With Quote
Old 03-08-2011, 12:40 AM   PM User | #5
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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]);
spchinta is offline   Reply With Quote
Old 03-08-2011, 01:06 AM   PM User | #6
SYNACK
New to the CF scene

 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
SYNACK is an unknown quantity at this point
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();
		}
    }
}
SYNACK is offline   Reply With Quote
Old 03-08-2011, 05:54 PM   PM User | #7
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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.
spchinta is offline   Reply With Quote
Old 03-08-2011, 06:47 PM   PM User | #8
SYNACK
New to the CF scene

 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
SYNACK is an unknown quantity at this point
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.
SYNACK is offline   Reply With Quote
Old 03-09-2011, 05:32 PM   PM User | #9
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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..
spchinta is offline   Reply With Quote
Old 03-09-2011, 06:11 PM   PM User | #10
SYNACK
New to the CF scene

 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
SYNACK is an unknown quantity at this point
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)
SYNACK is offline   Reply With Quote
Old 03-09-2011, 09:38 PM   PM User | #11
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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..
spchinta is offline   Reply With Quote
Old 03-10-2011, 07:14 PM   PM User | #12
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
IS this resolved or still having issues?
spchinta is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:10 PM.


Advertisement
Log in to turn off these ads.