1
import java.util.*;

public class Test
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Random rand = new Random();

        System.out.print("Want to play? ('Y' or 'N'): ");
        String want = input.nextLine();

        double money;
        String word1 = "", word2 = "", word3 = "";
        int randInt1, randInt2, randInt3;

        double totalMoney = 0.0, totalEarnings = 0.0;

        do
        {
            System.out.print("How much money do you want to enter? $");
            money = input.nextDouble();
            totalMoney += money;

            randInt1 = rand.nextInt(6);

            if (randInt1 == 0)
            {
                word1 = "Cherries";
            }
            else if (randInt1 == 1)
            {
                word1 = "Oranges";
            }
            else if (randInt1 == 2)
            {
                word1 = "Plums";
            }
            else if (randInt1 == 3)
            {
                word1 = "Bells";
            }
            else if (randInt1 == 4)
            {
                word1 = "Melons";
            }
            else
            {
                word1 = "Bars";
            }

            randInt2 = rand.nextInt(6);

            if (randInt2 == 0)
            {
                word2 = "Cherries";
            }
            else if (randInt2 == 1)
            {
                word2 = "Oranges";
            }
            else if (randInt2 == 2)
            {
                word2 = "Plums";
            }
            else if (randInt2 == 3)
            {
                word2 = "Bells";
            }
            else if (randInt2 == 4)
            {
                word2 = "Melons";
            }
            else
            {
                word2 = "Bars";
            }

            randInt3 = rand.nextInt(6);

            if (randInt3 == 0)
            {
                word3 = "Cherries";
            }
            else if (randInt3 == 1)
            {
                word3 = "Oranges";
            }
            else if (randInt3 == 2)
            {
                word3 = "Plums";
            }
            else if (randInt3 == 3)
            {
                word3 = "Bells";
            }
            else if (randInt3 == 4)
            {
                word3 = "Melons";
            }
            else
            {
                word3 = "Bars";
            }

            System.out.println(word1 + "  " + word2 + "  " + word3);

            if (word1.equals(word2) && word1.equals(word3))
            {
                System.out.printf("You won $%.2f\n", (money * 3));
                totalEarnings += (money * 3);
            }
            else if ((word1.equals(word2) && !word1.equals(word3)) || (!word1.equals(word2) && word1.equals(word3)))
            {
                System.out.printf("You won $%.2f\n", (money * 2));
                totalEarnings += (money * 2);
            }
            else if ((word2.equals(word1) && !word2.equals(word3)) || (!word2.equals(word1) && word2.equals(word3)))
            {
                System.out.printf("You won $%.2f\n", (money * 2));
                totalEarnings += (money * 2);
            }
            else if ((word2.equals(word1) && !word2.equals(word3)) || (!word2.equals(word1) && word2.equals(word3)))
            {
                System.out.printf("You won $%.2f\n", (money * 2));
                totalEarnings += (money * 2);
            }
            else
            {
                System.out.println("You won $0");
            }

            System.out.print("\nWant to play again? ('Y' or 'N'): ");
            want = input.nextLine();
        }
        while (want.equalsIgnoreCase("Y"));

        System.out.printf("\n\nYou entered a total of $%.2f into the slot machine.\n", totalMoney);
        System.out.printf("Your total earnings are $%.2f", totalEarnings);
    }
}

I'm having trouble trying to get this while loop to repeat. I included the sentinel value of user input "Y" or "N" ("N" for stop). In the while loop, it asks user to enter the sentinel again. But this part is not executing when I run the program.

In the console, the last lines shown are:

Want to play again? ('Y' or 'N'):

You entered a total of $15.00 into the slot machine.

Your total earnings are $0.00

So the last input for "Y" or "N" is not executing.

Ansar Al
  • 39
  • 6
  • 2
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Isaiah May 06 '20 at 17:34
  • Just an observation, your code would be cleaner with swtiches rather than if and else if's (https://www.w3schools.com/java/java_switch.asp) – Zach Rieck May 06 '20 at 17:46
  • Why dont you print the value of want before the end of the loop? – NomadMaker May 06 '20 at 18:28
  • 1
    @NomadMaker why would I need to print the value of "want"? That variable just contains whether the user wants to carry on playing. – Ansar Al May 06 '20 at 21:22
  • 1
    Because it obviously doesn't contain what you think it does, and printing such things is one way of debugging. – NomadMaker May 06 '20 at 21:32
  • @NomadMaker I see, thanks – Ansar Al May 06 '20 at 22:05

1 Answers1

1

You are not consuming the "Enter" with your code

    money = input.nextDouble();

Then when you get to your code

    want = input.nextLine();

an empty string is returned.

Place the following code after your code for input.nextDouble()

        input.nextLine();

This will consume the "Enter" from when you asked for the bet amount.