Ansar Al
Ansar Al

Reputation: 149

While loop is not repeating in Java

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.

Upvotes: 1

Views: 103

Answers (1)

Tom Schorsch
Tom Schorsch

Reputation: 66

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.

Upvotes: 1

Related Questions