我想写一个程序,我掷两个骰子,直到我得到那些骰子的总和为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.");
}
}
Run Code Online (Sandbox Code Playgroud)
它只是保持打印,需要1卷才能获得11的总和,所以有些事情是不对的.
我的目标:让玩家1继续滚动2直到我得到11的总和,并记录它花了多少尝试.然后让玩家2也这样做.然后,任何具有较低尝试次数的玩家"赢得"游戏.
帮助赞赏谢谢我新手
您可能想要更新您的情况
while (P1sum == 11) // this results in false and exit anytime your sum is not 11
Run Code Online (Sandbox Code Playgroud)
至
while (P1sum != 11) // this would execute the loop until your sum is 11
Run Code Online (Sandbox Code Playgroud)