而循环在Java中不重复

而循环在Java中不重复,java,while-loop,Java,While Loop,我很难让这个while循环重复。 我包括了用户输入“Y”或“N”(“N”表示停止)的前哨值。 在while循环中,它要求用户再次输入哨兵。但是当我运行程序时,这部分没有执行 在控制台中,显示的最后几行是: 想再玩一次吗?(“是”或“否”): 您在老虎机中总共输入了15.00美元 你的总收入是0.00美元 因此,“Y”或“N”的最后一个输入没有执行。您没有在代码中使用“Enter” import java.util.*; public class Test { public static

我很难让这个while循环重复。 我包括了用户输入“Y”或“N”(“N”表示停止)的前哨值。 在while循环中,它要求用户再次输入哨兵。但是当我运行程序时,这部分没有执行

在控制台中,显示的最后几行是:

想再玩一次吗?(“是”或“否”):

您在老虎机中总共输入了15.00美元

你的总收入是0.00美元

因此,“Y”或“N”的最后一个输入没有执行。

您没有在代码中使用“Enter”

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);
    }
}
然后当你进入你的代码

    money = input.nextDouble();
返回一个空字符串

将以下代码放在
input.nextDouble()的代码之后


这将消耗您询问下注金额时的“回车”功能。

这是否回答了您的问题?只是一个观察,你的代码使用swtices比if和else if更干净()为什么不在循环结束之前打印want的值?@NomadMaker为什么我需要打印want的值?该变量只包含用户是否希望继续播放,因为它显然不包含您认为它的功能,而打印这些内容是调试的一种方式。
    want = input.nextLine();
        input.nextLine();