Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com How do I print out a list of already guessed numbers?

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-06-2011, 07: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 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.
 03-08-2011, 01:02 AM PM User | #2 spchinta 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?");
 03-08-2011, 01: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 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.
 03-08-2011, 01:36 AM PM User | #4 spchinta 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..
 03-08-2011, 01:40 AM PM User | #5 spchinta 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]);```
 03-08-2011, 02: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 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(); } } }```
 03-08-2011, 06:54 PM PM User | #7 spchinta 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.
 03-08-2011, 07: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 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.
 03-09-2011, 06:32 PM PM User | #9 spchinta 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 07:16 PM..
 03-09-2011, 07: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 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)```
 03-09-2011, 10:38 PM PM User | #11 spchinta 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..
 03-10-2011, 08:14 PM PM User | #12 spchinta 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?

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 09:34 AM.