1

this is a simple Craps game, I am confused on why the code prompts the user a question twice at the end. It only prompts the user the question twice if I say (y) to set a bet. Output Do you want to play craps? (y/n) y Your current cash pot is: $100

            Would you like to bet? (y/n) n
            Press "ENTER" to continue...

            Rolling...
            Dice 1: 1
            Dice 2: 1
            Total: 2
            You Lost... :(
            Your total now is $100

            Do you want to play craps? (y/n) 
            y
            Your current cash pot is: $100

            Would you like to bet? (y/n) n
            Press "ENTER" to continue...

            Rolling...
            Dice 1: 2
            Dice 2: 6
            Total: 8
            {Your POINT is: 8}

            Rolling for the Point: 8
            Press "ENTER" to continue...

            Dice 1: 3
            Dice 2: 2
            {Total: 5}
            Rolling for the Point: 8
            Press "ENTER" to continue...

            Dice 1: 1
            Dice 2: 5
            {Total: 6}
            Rolling for the Point: 8
            Press "ENTER" to continue...

            Dice 1: 6
            Dice 2: 1
            {Total: 7}
            You Lost...
            Your total now is $100

            Do you want to play craps? (y/n) 
            y
            Your current cash pot is: $100

            Would you like to bet? (y/n) y
            How much would you like to bet? $5
            Your bet stands at $5
            Press "ENTER" to continue...

            Rolling...
            Dice 1: 2
            Dice 2: 6
            Total: 8
            {Your POINT is: 8}

            Rolling for the Point: 8
            Press "ENTER" to continue...

            Dice 1: 5
            Dice 2: 4
            {Total: 9}
            Rolling for the Point: 8
            Press "ENTER" to continue...

            Dice 1: 5
            Dice 2: 3
            {Total: 8}
            You Won!!!
            Your total now is $105

            Do you want to play craps? (y/n) 

            Do you want to play craps? (y/n) 
            y
            Your current cash pot is: $105

            Would you like to bet? (y/n) y
            How much would you like to bet? $5
            Your bet stands at $5
            Press "ENTER" to continue...

            Rolling...
            Dice 1: 5
            Dice 2: 6
            Total: 11
            You Won!! :)
            Your total now is $110

            Do you want to play craps? (y/n) 

            Do you want to play craps? (y/n) 
            y
            Your current cash pot is: $110

            Would you like to bet? (y/n) y
            How much would you like to bet? $5
            Your bet stands at $5
            Press "ENTER" to continue...

            Rolling...
            Dice 1: 4
            Dice 2: 5
            Total: 9
            {Your POINT is: 9}

            Rolling for the Point: 9
            Press "ENTER" to continue...

            Dice 1: 6
            Dice 2: 3
            {Total: 9}
            You Won!!!
            Your total now is $115

            Do you want to play craps? (y/n) 

            Do you want to play craps? (y/n) 
            y
            Your current cash pot is: $115

            Would you like to bet? (y/n) n
            Press "ENTER" to continue...

            Rolling...
            Dice 1: 5
            Dice 2: 5
            Total: 10
            {Your POINT is: 10}

            Rolling for the Point: 10
            Press "ENTER" to continue...

            Dice 1: 5
            Dice 2: 3
            {Total: 8}
            Rolling for the Point: 10
            Press "ENTER" to continue...

            Dice 1: 1
            Dice 2: 1
            {Total: 2}
            Rolling for the Point: 10
            Press "ENTER" to continue...

            Dice 1: 4
            Dice 2: 2
            {Total: 6}
            Rolling for the Point: 10
            Press "ENTER" to continue...

            Dice 1: 5
            Dice 2: 1
            {Total: 6}
            Rolling for the Point: 10
            Press "ENTER" to continue...

            Dice 1: 2
            Dice 2: 5
            {Total: 7}
            You Lost...
            Your total now is $115

            Do you want to play craps? (y/n) 
            n
            Good bye.
            ----Statistics----
            Won:  3
            Lost: 4
            Cash: $115

Here below is the source I am running: package project3;

            import java.util.Random;    // Used for generating a random number dice
            import java.util.Scanner;   // Used for getting input from user

            /**
             *
             * @author Sartaj Singh
             */
            public class Project3 {

                /**
                 *
                 */
                public static void promptEnterKey() {
                    System.out.println("Press \"ENTER\" to continue...");
                    Scanner scanner = new Scanner(System.in);
                    scanner.nextLine();
                }

                public static void main(String[] args) {
                    int pot = 100;
                    int bet = 0;
                    int wins = 0;
                    int losses = 0;
                    //  int games = 0;

                    // Make an instance of the Random and Scanner Class
                    Random random = new Random();
                    Scanner input = new Scanner(System.in);
                    while (pot >= 1) {
                        System.out.println();
                        System.out.println("Do you want to play craps? (y/n) ");
                        String play_choice = input.nextLine();
                        if ("N".equals(play_choice) || "n".equals(play_choice)) {
                            System.out.println("Good bye.");
                            System.out.println("----Statistics----");
                            System.out.println("Won:  " + wins);
                            System.out.println("Lost: " + losses);
                            System.out.println("Cash: $" + pot);
                            break;
                        }

                        else if ("Y".equals(play_choice) || "y".equals(play_choice)) {
                            System.out.println("Your current cash pot is: $" + pot);
                            System.out.println();
                            System.out.print("Would you like to bet? (y/n) ");
                            String bet_choice = input.nextLine();
                            if ("y".equals(bet_choice) || "Y".equals(bet_choice)) {
                                System.out.print("How much would you like to bet? $");
                                bet = input.nextInt();
                                while (bet < 1 || bet > pot) {
                                    System.out.print("YOU CANT BET THAT AMOUNT! How much would you like to bet? $");
                                    bet = input.nextInt();
                                }
                                System.out.println("Your bet stands at $" + bet);

                            }
                            if ("n".equals(bet_choice) || "N".equals(bet_choice)) {
                                bet = 0;
                            }
                            promptEnterKey();
                            System.out.println("Rolling...");
                            int dice1;
                            dice1 = random.nextInt(6) + 1;
                            int dice2;
                            dice2 = random.nextInt(6) + 1;
                            int dice_total = dice1 + dice2;
                            System.out.println("Dice 1: " + dice1);
                            System.out.println("Dice 2: " + dice2);
                            System.out.println("Total: " + dice_total);
                            switch (dice_total) {
                                case 7:
                                case 11:
                                    System.out.println("You Won!! :)");
                                    pot += bet;
                                    wins++;
                                    System.out.println("Your total now is $" + pot);
                                    break;
                                case 2:
                                case 3:
                                case 12:
                                    System.out.println("You Lost... :(");
                                    pot -= bet;
                                    losses++;
                                    System.out.println("Your total now is $" + pot);
                                    break;
                                default:
                                    int point = dice_total;
                                    System.out.println("{Your POINT is: " + point + "}");
                                    System.out.println("");
                                    while (dice_total != 7) {
                                        System.out.println("Rolling for the Point: " + point);
                                        promptEnterKey();
                                        dice1 = random.nextInt(6) + 1;
                                        dice2 = random.nextInt(6) + 1;
                                        dice_total = dice1 + dice2;
                                        System.out.println("Dice 1: " + dice1);
                                        System.out.println("Dice 2: " + dice2);
                                        System.out.println("{Total: " + dice_total + "}");
                                        if (dice_total == point) {
                                            System.out.println("You Won!!!");
                                            pot += bet;
                                            wins++;
                                            System.out.println("Your total now is $" + pot);
                                            break;
                                        } else if (dice_total == 7) {
                                            System.out.println("You Lost...");
                                            pot -= bet;
                                            losses++;
                                            System.out.println("Your total now is $" + pot);
                                            break;
                                        }
                                    }
                            }
                        }
                    }
                }
            }

If you need any other information, please let me know. I really want to learn why my code is doing this? The entire program works fine, but prompting the user twice with the same question is a little redundant.

1 Answers1

1

After doing bet = input.nextInt(); you should also do input.nextLine();.

Here is the changed code snippet:

if ("y".equals(bet_choice) || "Y".equals(bet_choice)) {
    System.out.print("How much would you like to bet? $");
    bet = input.nextInt();
    while (bet < 1 || bet > pot) {
        System.out.print("YOU CANT BET THAT AMOUNT! How much would you like to bet? $");
        bet = input.nextInt();
    }
    /* Add Here */
    input.nextLine();
    System.out.println("Your bet stands at $" + bet);
}
user2004685
  • 8,721
  • 4
  • 29
  • 49