Skip to content
Advertisement

Throws Exception when trying to break loop because a condition has been met

I am trying to figure out why I cannot get the loop to break if either “n” is input for playAgain or when the total is below $10. If you see how I can break the loop to run the gameOver function without having an exception thrown that would be a great help. I have noted in the code below that I am having trouble with. I am unsure why this exception is being thrown. If you are able to find out how to break the loop when total is less than 10 or when playAgain is false please let me know!

import java.util.Random;
import java.util.Scanner;

public class GameOfCrapsTester {
    
    static Scanner in = new Scanner(System.in);
    static Random rand = new Random();

    public static void main(String[] args) {
        
        System.out.println("Welcome to the game of Craps");
        System.out.println(" ");
        System.out.println("The house has given you a starting balance of $500");
        System.out.println("On each round, you will make a whole number wager.");
        System.out.println("The minimum wager is $10, and the maximum wager is your remaining balance.");
        System.out.println(" ");
        System.out.println("You may keep playing until you decide to cash in, or");
        System.out.println("    you can't cover the minimum wager.");
        System.out.println("Good Luck!");
        
        boolean win;
        double wins = 0, numOfGames = 0;
        int total = 500;    

        // Come out roll and set point value
        int pointValue = 0;
        boolean playAgain = true;
        while (playAgain && total > 0)
        {
            System.out.println(" ");
            System.out.println("Your balance is $" + total);
            System.out.println(" ");
            System.out.println("Place your bet: $");
            
            // Get and check wager placed
            int bet = in.nextInt();
            while (bet > total || bet < 10)
            {
                if (bet < 10)
                {
                    System.out.println("Bet must be larger than $10.");
                }
                System.out.println("I'm sorry, that's not a valid wager; please re-enter: ");
                bet = in.nextInt();
            }
            int num = rollDice();
            if ((num >= 4 && num <= 10 && num != 7) || num == 0)
            {
                pointValue = num; 
                System.out.println(" ");
                System.out.println("Your point value is " + pointValue);
                System.out.println(" ");
                win = rollWithPoint(pointValue);
                
                if (win)
                {
                    total = wonGame(bet, total);
                    wins++;
                    numOfGames++;
                    System.out.println("Wins: " + wins + " Number of games: " + numOfGames);
                }
                else if (!win)
                {
                    total = lostGame(bet, total);
                    numOfGames++;
                    System.out.println("Wins: " + wins + " Number of games: " + numOfGames);
                }   
            }
            else if (num == 7 || num == 11)
            {
                total = wonGame(bet, total);
                wins++;
                numOfGames++;
                System.out.println("Wins: " + wins + " Number of games: " + numOfGames);
            }
            else
            {
                total = lostGame(bet, total);
                numOfGames++;
                System.out.println("Wins: " + wins + " Number of games: " + numOfGames);
            }
            
            if (total <= 9) // THIS IS WHERE I AM HAVING TROUBLE
            {
                break;
            }
            
            System.out.println("Keep playing (y/Y or n/N)? ");
            in.nextLine();
            String again = in.nextLine();
            
            if (again.equalsIgnoreCase("y"))
            {
                playAgain = true;
            }
            else if (again.equalsIgnoreCase("n")) // THIS IS WHERE I AM HAVING TROUBLE
            {
                break;
            }
            else
            {
                System.out.println("Invalid character input, try again:");
                again = in.nextLine();
            }
        }// end of loop
        
        gameOver(wins, numOfGames);
        
    } // END of main
    
    public static int rollDice() {
        
        int dice1, dice2, total;
        dice1 = rand.nextInt(6) + 1;
        dice2 = rand.nextInt(6) + 1;
        total = dice1 + dice2;
        System.out.print("Your roll: ");
        System.out.print("Dice1: " + dice1);
        System.out.print(", Dice2: " + dice2);
        System.out.println("; Roll Value: " + total);
        return total;
        
    } // END of rollDice
    
    public static boolean rollWithPoint(int point) {
        
        int total = rollDice();
        boolean winner = false;
        while(total != 7 && winner == false)
        {
            total = rollDice();
            if (total == point)
            {
                winner = true;
            }
            else
            {
                winner = false;
            }
        }
        return winner;
    } // END of rollWithPoint

    public static int lostGame(int bet, int total) {
        
        System.out.println("Oh, I'm sorry, you lost.");
        System.out.println(" ");
        total = total - bet;
        System.out.println("Your current balance: $" + total);
        System.out.println(" ");
        return total;
        
    } // END of lostGame
    
    public static int wonGame(int bet, int total) {
        
        System.out.println("A winner!");
        System.out.println(" ");
        total = total + bet;
        System.out.println("Your current balance: $" + total);
        System.out.println(" ");
        return total;
        
    } // END of wonGame
    
    public static void gameOver(double win, double tot) {
        
        double winPercent = (win / tot) * 100;
        System.out.println(" ");
        System.out.printf("Based on your play, the probability of winning is %.2%", winPercent);
        System.out.println(" ");
        System.out.println("Seems you lost your shirt; better luck next time.");
        System.out.println("Have a nice day! Hope to see you soon!");
        
    } // END of gameOver
    
} // END of GameOfCraps

Advertisement

Answer

There is no error when you change this (without using String.format()):

System.out.printf("Based on your play, the probability of winning is %.2%", winPercent);

To this:

System.out.println("Based on your play, the probability of winning is " + winPercent + "%.");

Example with a little bet (console):

Your balance is $11
 
Place your bet: $
10
Your roll: Dice1: 1, Dice2: 2; Roll Value: 3
Oh, I'm sorry, you lost.
 
Your current balance: $1
 
Wins: 0.0 Number of games: 2.0
 
Based on your play, the probability of winning is 0.0%.
 
Seems you lost your shirt; better luck next time.
Have a nice day! Hope to see you soon!

I cannot get the loop to break if either “n” is input for playAgain or when the total is below $10.

It works fine too. If I put a bet below 10 it asks me to put another bit. What

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement