首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java:掷两个骰子直到你得到想要的总数

Java:掷两个骰子直到你得到想要的总数
EN

Stack Overflow用户
提问于 2017-08-09 12:30:48
回答 1查看 1.9K关注 0票数 4

我想写一个程序,我掷两个骰子,直到我得到这些骰子的总和是11。我想记录下当我得到11的总和时,我使用了多少次“尝试”或掷骰子。

到目前为止我的代码如下:

代码语言:javascript
复制
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做同样的事情。然后,尝试次数较少的玩家将“赢得”游戏。

感谢帮助,谢谢我是个新手

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-09 12:33:55

你可能想要更新你的情况

代码语言:javascript
复制
while (P1sum == 11) // this results in false and exit anytime your sum is not 11

代码语言:javascript
复制
while (P1sum != 11) // this would execute the loop until your sum is 11
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45581721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档