我想编写一个程序,我掷两个骰子,直到我得到这些骰子的总和为 11。我想记录在我得到总和时我使用了多少次“尝试”或掷骰子11.
到目前为止我的代码:
public class Dice {
public static void main(String[] args) {
int counter1 = 0; //Amount of rolls player 1 took to get sum of 9
int P1sum = 0;
do {
int P1Die1 = (int)(Math.random()*6)+1;
int P1Die2 = (int)(Math.random()*6)+1;
P1sum = (P1Die1 + P1Die2);
counter1++;
} while (P1sum == 11);
System.out.println("Player 1 took "+counter1+" amount of rolls to have a sum of 11.");
}
}
它只是不断地打印出需要 1 卷才能得到 11 的总和,所以有些地方不对。
我的目标:让玩家 1 继续掷骰子 2,直到我的总和为 11,并记录它尝试了多少次。然后让玩家 2 做同样的事情。然后,无论哪个玩家的尝试次数较少,都会“赢得”游戏。
帮助感谢感谢我是新手
最佳答案
你可能想更新你的条件
while (P1sum == 11) // this results in false and exit anytime your sum is not 11
到
while (P1sum != 11) // this would execute the loop until your sum is 11
关于Java:掷两个骰子直到得到想要的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45581721/