Results 1 to 5 of 5
Thread: Guessing Game Loop
- 08-14-2012, 03:08 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Guessing Game Loop
So I'm having trouble looping this code. I want to make it so if the user has more than 00.00 in the account then they will be able to keep playing without resetting the AmountInAccount.
Anyone have any tips?
Java Code:import static java.lang.System.out; import java.util.Scanner; import java.util.Random; public class Bets { public static void main (String args [ ]) { Scanner keyboard = new Scanner (System.in) ; double AmountInAccount; AmountInAccount=0.00; AmountInAccount=AmountInAccount + 1000.00; System.out.println ("Welcome."); System.out.print("You have $"); System.out.print(AmountInAccount); System.out.println(" in your account."); out.println ("Enter the amount you wish to bet"); int inputBet = keyboard.nextInt ( ); out.print ("Betting "); System.out.println (inputBet); out.println ("Enter a number between 1 and 10"); int inputNumber = keyboard.nextInt ( ); int randomNumber = new Random ( ) .nextInt (10) + 1; while (inputBet > AmountInAccount) { out.println (); out.println ("You don't have enough Money..!"); out.println ("Please enter a bet you can afford!"); int inputBets = keyboard.nextInt ( ); } if (inputNumber == randomNumber) { out.println ("**You Win!**"); out.println ("Your bet has been added to your account"); out.print ("$"); System.out.println (AmountInAccount + inputBet * 10); out.println (" has been added to your account."); } else { out.println ("You Lose"); out.println ("Because you suck (:"); out.print ("The Random Number was "); out.println (randomNumber + "."); out.print ("You have $"); System.out.print (AmountInAccount - inputBet); out.println (" left in your account."); } if (AmountInAccount == 00.00) { out.println ("Game Over - You lose"); out.println ("Please play again"); } else { out.println (""); } } }Last edited by co2balance; 08-14-2012 at 03:19 PM.
- 08-14-2012, 03:23 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
Re: Guessing Game Loop
This translates almost naturally to;
You shoudn't stick all that code in your main( ... ) method, otherwise you'll end up with ugly wallpaper full of code dangling from left to right and vice versa and all the way down ad nauseam.Java Code:while (AmountInAccount > 0) { ... }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-14-2012, 03:27 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
- 08-14-2012, 03:45 PM #4
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
- 08-14-2012, 04:49 PM #5
Re: Guessing Game Loop
You would place your while statement at the end of your code... usually. Depends on what you want to do. But, for this game, the while loop should be where your only while loop is present.
My suggestion would be to make a public void game() { ... <insert game code here> ... }, as well as a public void decideEnd() { ... <insert end of game code here> ... } and have the while loop call it.
That way, once the game has gotten the AmountInAccount less than 0.0, the while loop is broken and the last part of the game ( decideEnd(); ) will be called and the program exits after that point. ALSO! MakeJava Code:while (AmountInAccount > 0.0) { game(); } decideEnd();
Global... if you know what I mean. Put them outside of the methods, so that any method in this class can alter then as you specify. Otherwise, the game() method and decideEnd() methods will not be able to access and modify these variables during runtime due to those variables being out of their range.Java Code:Scanner keyboard = new Scanner (System.in) ; double AmountInAccount; AmountInAccount=0.00; AmountInAccount=AmountInAccount + 1000.00;
Lastly, I might be wrong here, but I do believe that most Java Compilers import java.lang.* automatically at compile time. Which means that everything in the java.lang class field is automatically put into your code unless explicitly stated otherwise. So explicitly stating import static java.lang.System.out; is not neccessary. You still need the java.util though. I suggest making all of your out.print or out.println --> System.out.print or System.out.println.My API:Java Code:cat > a.out || cat > main.class
Similar Threads
-
guessing game
By MrM in forum New To JavaReplies: 1Last Post: 02-02-2011, 07:51 PM -
Guessing Game with for loop
By ccart62 in forum New To JavaReplies: 3Last Post: 11-22-2010, 12:55 AM -
Need help in Guessing Game
By rose in forum Java GamingReplies: 4Last Post: 10-27-2010, 10:43 PM -
guessing game help
By yasmin k in forum AWT / SwingReplies: 4Last Post: 10-31-2009, 05:37 PM -
guessing game using GUI
By yasmin k in forum New To JavaReplies: 1Last Post: 10-26-2009, 12:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks