java - Java中的while循环不重复
问题描述
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);
}
}
我在尝试让这个 while 循环重复时遇到了麻烦。我包括了用户输入“Y”或“N”(“N”表示停止)的标记值。在 while 循环中,它要求用户再次输入哨兵。但是当我运行程序时,这部分没有执行。
在控制台中,显示的最后几行是:
想再玩吗?(“Y”或“N”):
您在老虎机中输入了总计 15.00 美元。
您的总收入为 0.00 美元
所以“Y”或“N”的最后一个输入没有执行。
解决方案
您没有使用您的代码使用“Enter”
money = input.nextDouble();
然后当你得到你的代码
want = input.nextLine();
返回一个空字符串。
将以下代码放在您的代码之后input.nextDouble()
input.nextLine();
这将消耗您要求下注金额时的“输入”。
推荐阅读
- java - How do I reorder a linked list?
- java - 在将输入作为变量从java传递给存储过程之前对其进行清理
- javascript - 如何使用 javascript 或 jquery 在单个文本字段中验证多个电子邮件地址
- r - 您如何在嵌套在 tibbles 中的许多模型上执行 predict.gam?
- python - 本地主机上的 Django enctype=multipart/form-data 返回白屏
- r - 编写一个 R 函数来检查一个矩阵是否只包含 0 和 1
- php - 如何在 MVC 中使用 $_GET 从 url 获取数据?
- php - 如何计算跨数据库行的平均值?
- python - Matplotlib 错误栏更改帽子样式
- javascript - 在新选项卡中显示来自 Azure Blob 存储容器的 PDF 文件