我想写一个程序,我掷两个骰子,直到我得到这些骰子的总和是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做同样的事情。然后,尝试次数较少的玩家将“赢得”游戏。
感谢帮助,谢谢我是个新手
发布于 2017-08-09 12:33:55
你可能想要更新你的情况
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
https://stackoverflow.com/questions/45581721
复制相似问题