Java:掷两个骰子直到得到想要的总和

标签 java dice

我想编写一个程序,我掷两个骰子,直到我得到这些骰子的总和为 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/

相关文章:

java - 如何从随机数系列中排除某个数字?

java - 骰子声明错误

java - 无法从 firebase 检索图像

Java:catch语句中的标识符起什么作用?

java - 字符串包含相同的字符但仍然不同

java - 使 Math.Random 循环,直到达到特定数字

algorithm - 在掷骰子算法方面需要一些帮助

java - 使用文本框作为 JList 的输入

java - 部署应用程序时出现 ClassNotFoundException 时如何处理 hazelcast

c++ - 在 C++ 中滚动多个模具