[英]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.我在尝试让这个 while 循环重复时遇到了麻烦。 I included the sentinel value of user input "Y" or "N" ("N" for stop).
我包括了用户输入“Y”或“N”(“N”表示停止)的标记值。 In the while loop, it asks user to enter the sentinel again.
在 while 循环中,它要求用户再次输入哨兵。 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'):
(“Y”或“N”):
You entered a total of $15.00 into the slot machine.您在老虎机中输入了总计 15.00 美元。
Your total earnings are $0.00您的总收入为 0.00 美元
So the last input for "Y" or "N" is not executing.所以“Y”或“N”的最后一个输入没有执行。
You are not consuming the "Enter" with your code您没有使用您的代码使用“Enter”
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.nextDouble()
代码之后
input.nextLine();
This will consume the "Enter" from when you asked for the bet amount.这将消耗您要求下注金额时的“输入”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.