我正在创建一个骰子游戏,其中 4 个骰子在数字 0-6 中滚动。掷出后,我想问用户是否要重新掷出 0、1 或 2 个骰子。我在更新 d1、d2、d3、d4(相应的骰子)变量时遇到问题,我不确定如何正确构建一个 while 循环来实现我的目标。我尝试注释掉的代码,因为它不起作用。它会重新询问用户,并且永远不会转义 while 循环。它也不会更新所选骰子的变量。
非常感谢您的任何帮助。
我已经尝试过 if、if-else、while、do-while 循环和语句。我认为我没有正确构建它们。
public class Program6b {
public static void main (String[]args)
{
Scanner stdIn = new Scanner(System.in);
// This next section is an introduction to what my program does. \
System.out.println("Welcome to Coulter's Dice Game!");
System.out.println("---------------------------------------------");
System.out.println("Any Quad and you win! (106 wins)");
System.out.println("Any Triple and you win! (6 wins)");
System.out.println("Any Two-Pair is a win! (4 wins)");
System.out.println("Anything else and you lose. (1 loss)");
System.out.println("---------------------------------------------");
System.out.println();
System.out.println("Type anything to roll your first die.");
String dontMindMe = "";
dontMindMe = stdIn.next();
System.out.println();
System.out.println("---------------------------------------------");
System.out.println();
int d1, d2, d3, d4; // Int variables for 4 die.
int wins = 0, loses = 0; // Keeps track of wins and loses.
int rounds = 0; // Keeps track of rounds played.
String playAgain = ""; // String to store if the user would like to play again.
boolean win = false; // Boolean to determine if the player one.
String rerollOne, rerollTwo;
do
{
rounds = rounds + 1; // Adds 1 round every time the program is looped.
d1 = (int)(Math.random() * 6) + 1;
d2 = (int)(Math.random() * 6) + 1;
d3 = (int)(Math.random() * 6) + 1;
d4 = (int)(Math.random() * 6) + 1;
System.out.println(" Player ");
System.out.println("----------");
System.out.println(d1 + " " + d2 + " " + d3 + " " + d4); // Outputs the dice rolls.
System.out.println();
win = false; // Resets the win boolean to false.
// This next section asks the user if they would like to re-roll their dice
int choice;
/* System.out.println("How many dice would you like to reroll? (0,1,2): ");
choice = stdIn.nextInt();
while (choice != 0)
{
if (choice == 1)
{
System.out.println("What is the one die you would like to reroll? (d1, d2, d3, d4): ");
rerollOne = stdIn.next();
System.out.println();
if (rerollOne == "d1")
{
d1 = (int)(Math.random() * 6) + 1;
}
else if (rerollOne == "d2")
{
d2 = (int)(Math.random() * 6) + 1;
}
else if (rerollOne == "d3")
{
d3 = (int)(Math.random() * 6) + 1;
}
else if (rerollOne == "d4")
{
d4 = (int)(Math.random() * 6) + 1;
}
System.out.println(" Player ");
System.out.println("----------");
System.out.println(d1 + " " + d2 + " " + d3 + " " + d4); // Outputs the dice rolls.
System.out.println();
}
else if (choice == 2)
{
System.out.println("What is the first die you would like to reroll? (d1, d2, d3, d4): ");
rerollOne = stdIn.next();
System.out.println();
if (rerollOne == "d1")
{
d1 = (int)(Math.random() * 6) + 1;
}
else if (rerollOne == "d2")
{
d2 = (int)(Math.random() * 6) + 1;
}
else if (rerollOne == "d3")
{
d3 = (int)(Math.random() * 6) + 1;
}
else if (rerollOne == "d4")
{
d4 = (int)(Math.random() * 6) + 1;
}
System.out.println("What is the second die you would like to reroll? (d1, d2, d3, d4): ");
rerollTwo = stdIn.next();
System.out.println();
if (rerollTwo == "d1")
{
d1 = (int)(Math.random() * 6) + 1;
}
else if (rerollTwo == "d2")
{
d2 = (int)(Math.random() * 6) + 1;
}
else if (rerollTwo == "d3")
{
d3 = (int)(Math.random() * 6) + 1;
}
else if (rerollTwo == "d4")
{
d4 = (int)(Math.random() * 6) + 1;
}
System.out.println(" Player ");
System.out.println("----------");
System.out.println(d1 + " " + d2 + " " + d3 + " " + d4); // Outputs the dice rolls.
System.out.println();
}
else
{
System.out.println("How many dice would you like to reroll? (0,1,2): ");
choice = stdIn.nextInt();
}
} */
// This next if, else if, and else statement determines what the player wins. \
if ( (d1 == d2) && (d2 == d3) && (d3 == d4) ) // Quad win (106 wins)
{
win = true;
wins = wins + 106;
System.out.println();
System.out.println("Quad win! (106 wins added)");
}
else if ( (d1 == d2) && (d2 == d3) ||
(d1 == d2) && (d2 == d4) ||
(d1 == d3) && (d3 == d4) ||
(d2 == d3) && (d3 == d4) ) // Triple win (6 wins)
{
win = true;
wins = wins + 6;
System.out.println();
System.out.println("Triple win! (6 wins added)");
}
else if (
(d1 == d2) && (d3 == d4) ||
(d1 == d3) && (d2 == d4) ||
(d1 == d4) && (d2 == d3) ) // Two pair win (4 wins)
{
win = true;
wins = wins + 4;
System.out.println();
System.out.println("Two-pair! (4 wins added)");
}
if ( win ) // If statement to print out if the user won.
{
System.out.println();
System.out.println("Yay! You won this dice round!");
System.out.println();
}
else
{
++loses; // Else statement to ass to the total loses and print out if the user lost.
System.out.println();
System.out.println("Bummer, that's a junker!");
System.out.println();
}
do // Do-while to make sure the user enters 'y' or 'n'.
{
System.out.println("Would you like to play again? (y/n): ");
playAgain = stdIn.next();
} while (!(playAgain.equalsIgnoreCase("y") || playAgain.equalsIgnoreCase("n")));
System.out.println(); // Creates an empty space to tidy the output up.
} while (playAgain.equalsIgnoreCase("y"));
// This next section displays the final total of wins and loses. \
System.out.println("---------------------------------------------");
System.out.println();
System.out.println("Here are your results:");
System.out.println("Total Rounds: " + (rounds));
System.out.println("Total Rounds Lost: " + loses);
System.out.println("Total Wins: " + wins);
}
}
我希望提示用户是否要重新掷 1 或 2 个骰子,以及程序分别工作。我希望该数字是与用户输入的内容相对应的更新。完成后,我希望向用户显示他们的胜利,并询问他们是否愿意再次玩。
您没有正确比较条件中的字符串。 由于使用==
运算符来比较字符串,因此代码正在检查引用是否相等。 在您的情况下,这将始终计算为 false。
例如,更改以下内容:
if (rerollOne == "d1")
对此:
if ("d1".equals(rerollOne))
equals 方法将比较字符串的内容。
此外,由于您只是在 else 块中提示输入新choice
,因此您将永远无法重新滚动。 我建议将新choice
的提示移到 else 之外,这样你就会在循环的每次迭代中提示一个新选择,然后完全删除 else 。