Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Stopping while loop if there are insufficient funds.

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Stopping while loop if there are insufficient funds.

    So basically, when someone's bankroll gets to 0 or below 0, I want the program to end so they cannot play any longer. Here is my current code:

    import java.util.*;
    public class Craps {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Want to play Craps? Type yes or no.");
    		String answer = scan.next();
    		System.out.println("How much is your bankroll?");
    		double bankroll = scan.nextDouble();
    		while (answer.equalsIgnoreCase("yes")) {
    			System.out.println("What is your bet?");
    			double betAmount = scan.nextDouble();
    			if (checkWin(rollDice()) == true) {
    				bankroll = bankroll + betAmount;
    				System.out.println("Your current bankroll is $" + bankroll);
    			} else {
    				bankroll = bankroll - betAmount;
    				System.out.println("Your current bankroll is $" + bankroll);
    			}
    			if (bankroll <=0) {
    				answer = "no";
    			}
    			System.out.println("Play again? Type yes or no: ");
    			answer = scan.next();	
    		}
    	}
    	public static double newBank(double betAmount, double bankroll) {
    		double newBank = bankroll - betAmount;
    		System.out.println(newBank);
    		return newBank;
    	}
    	public static int rollDice() {
    		Random randomNum = new Random();
    		int x = randomNum.nextInt(6) + 1;
    		int y = randomNum.nextInt(6) + 1;
    		System.out.println("Dice Rolls: " + x + " and " + y);
    		int comeoutRoll = x + y;
    		System.out.println("Your dice add up to: " + comeoutRoll);
    		return comeoutRoll;
    	}
     
    	public static boolean checkWin(int comeoutRoll) {
    		int firstRoll = comeoutRoll;
    		if (firstRoll == 7 | firstRoll == 11) {
    			System.out.println("Woah, winner here.");
    			return true;
    		} if (firstRoll == 2 | firstRoll == 3 | firstRoll == 12) {
    			System.out.println("You lost!");
    			return false;
    		} 
    		while (true) {
    			int currentRoll = rollDice();
    			if (currentRoll == 7) {
    				System.out.println("You lost!");
    				return false;
    			}
    			if (currentRoll == firstRoll) {
    				System.out.println("They match! You win!");
    				return true;
    			}
    				System.out.println("NOT QUITE - Continue rolling...");
    		}
    	}
    }

    Help would be wonderful!


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Stopping while loop if there are insufficient funds.

    Ok, is the play again input thing only if they have a bankroll greater than 0 or is it something that allows them to play again after they've lost or something and start over?

    (If it's the latter, it should be in an outer while loop.)


    Assuming that the two go together and that it will end if they type nor or if they hit a bankroll of 0, then you can use && to make sure both conditions are met for it to keep looping.


    i.e.

    while (answer.equalsIgnoreCase("yes") && bankroll > 0)
    {
    .........


    }

    What confuses me is why, if you set answer equal to "no", you allow the user to change the value of answer with your input into the variable answer.

  3. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    taze (October 16th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Stopping while loop if there are insufficient funds.

    I looked and I don't need the initial yes/no answer, so I got rid of it.

    So the goal is to allow them to choose play again as long as they have a positive bankroll, however once their bankroll is less than 0 (i.e. in the negatives) it is no longer an option. I'm not sure where I can put the question to ask yes or no to playing again.

    So here is my current code:
    import java.util.*;
    public class Craps {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		String answer = "yes";
    		System.out.println("How much is your bankroll?");
    		double bankroll = scan.nextDouble();
    		while (answer.equalsIgnoreCase("yes") && bankroll > 0) {
    			System.out.println("What is your bet?");
    			double betAmount = scan.nextDouble();
    			if (checkWin(rollDice()) == true) {
    				bankroll = bankroll + betAmount;
    				System.out.println("Your current bankroll is $" + bankroll);
    			} else {
    				bankroll = bankroll - betAmount;
    				System.out.println("Your current bankroll is $" + bankroll);
    			}
    			System.out.println("Play again? Type yes or no: ");
    			answer = scan.next();	
    		}      
    	}
    	public static double newBank(double betAmount, double bankroll) {
    		double newBank = bankroll - betAmount;
    		System.out.println(newBank);
    		return newBank;
    	}
    	public static int rollDice() {
    		Random randomNum = new Random();
    		int x = randomNum.nextInt(6) + 1;
    		int y = randomNum.nextInt(6) + 1;
    		System.out.println("Dice Rolls: " + x + " and " + y);
    		int comeoutRoll = x + y;
    		System.out.println("Your dice add up to: " + comeoutRoll);
    		return comeoutRoll;
    	}
     
    	public static boolean checkWin(int comeoutRoll) {
    		int firstRoll = comeoutRoll;
    		if (firstRoll == 7 | firstRoll == 11) {
    			System.out.println("Woah, winner here.");
    			return true;
    		} if (firstRoll == 2 | firstRoll == 3 | firstRoll == 12) {
    			System.out.println("You lost!");
    			return false;
    		} 
    		while (true) {
    			int currentRoll = rollDice();
    			if (currentRoll == 7) {
    				System.out.println("You lost!");
    				return false;
    			}
    			if (currentRoll == firstRoll) {
    				System.out.println("They match! You win!");
    				return true;
    			}
    				System.out.println("NOT QUITE - Continue rolling...");
    		}
    	}
    }

    I'm just not sure what part of the loop (or if I need another loop) that I should put the question that will allow them to redefine the variable answer and thus either end the game or continue on.

  5. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Stopping while loop if there are insufficient funds.

    If they type "no", does the game end?

    It seems that you're doing it right with the code above.


    I think a simple if statement should ensure it doesn't ask them to play again if their bankroll is 0.

    if (bankroll > 0)
    {
    System.out.println("Play again? Type yes or no: ");
    answer = scan.next();
    }


    and keep the question where you had it, just add the if statement.

    If the bankroll is 0, it will not go into the if and hence won't ask to play again and will exit the while loop (and, since I don't see anything after the while loop, the program will end.)

  6. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    taze (October 16th, 2013)

  7. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Stopping while loop if there are insufficient funds.

    Well that worked and I'm a little embarrassed I missed that! Thanks so much for the help.

Similar Threads

  1. Replies: 4
    Last Post: May 15th, 2011, 06:03 AM
  2. Stopping Graphics Animation.
    By SyntheticD in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 25th, 2011, 11:59 AM
  3. Starting and Stopping a loop with JButtons
    By Endevor in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 8th, 2010, 03:37 PM
  4. Stopping All Current Thread
    By newbie in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 08:27 PM
  5. While loop stopping?
    By Echo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 16th, 2010, 05:40 PM