Results 1 to 5 of 5
  1. #1
    co2balance is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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 04:19 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Guessing Game Loop

    Quote Originally Posted by co2balance View Post
    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?
    This translates almost naturally to;

    Java Code:
    while (AmountInAccount > 0) {
       ...
    }
    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.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    co2balance is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: Guessing Game Loop

    Quote Originally Posted by JosAH View Post
    This translates almost naturally to;

    Java Code:
    while (AmountInAccount > 0) {
       ...
    }
    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.

    kind regards,

    Jos
    I'm still very new (1 week or so into java) so a lot of my things are messy. I wrote a series of scripts and sort of patched them together to make this.
    Regardless, I apologise for the mess.

  4. #4
    co2balance is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: Guessing Game Loop

    Quote Originally Posted by JosAH View Post
    This translates almost naturally to;

    Java Code:
    while (AmountInAccount > 0) {
       ...
    }
    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.

    kind regards,

    Jos
    Where would you advise I insert the

    Java Code:
     while (AmountInAccount > 0.0) {
    I tried a few places and none of them function correctly.

  5. #5
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default 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.

    Java Code:
    while (AmountInAccount > 0.0) {
         game();
    }
    decideEnd();
    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! Make
    Java Code:
            Scanner keyboard = new Scanner (System.in) ;
            double AmountInAccount;
            AmountInAccount=0.00;
            AmountInAccount=AmountInAccount + 1000.00;
    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.

    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

  1. guessing game
    By MrM in forum New To Java
    Replies: 1
    Last Post: 02-02-2011, 08:51 PM
  2. Guessing Game with for loop
    By ccart62 in forum New To Java
    Replies: 3
    Last Post: 11-22-2010, 01:55 AM
  3. Need help in Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 11:43 PM
  4. guessing game help
    By yasmin k in forum AWT / Swing
    Replies: 4
    Last Post: 10-31-2009, 06:37 PM
  5. guessing game using GUI
    By yasmin k in forum New To Java
    Replies: 1
    Last Post: 10-26-2009, 01:13 PM

Posting Permissions

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