骰子滚动模拟

时间:2016-10-11 22:08:08

标签: java random

所以我迷路了。这是目标:

询问掷一个六面骰子的次数。 随机滚动这个骰子。 打印出一到六个每个数字出现的次数

这个循环的东西是否在正确的轨道上?我该怎么做才真的很抱歉

while(dice > 0)
{
    rolldice = gen.nextInt(6) + 1;  //(1-6)
    if (rolldice == 1)
    {
        one++;
    }
    else if (rolldice == 2)
    {
        two++;
    }
    else if (rolldice == 3)
    {
        three++;
    }
    else if (rolldice == 4)
    {
        four++;
    }
    else if (rolldice == 5)
    {
        five++;
    }
    else if (rolldice == 6)
    {
        six++;
    }
}

2 个答案:

答案 0 :(得分:0)

我假设骰子整数是来自用户的输入。如果你在每次迭代时减少骰子,你将掷骰子(完成循环)的次数与用户要求的次数相同。

不确定您是否已实现它,以便用户可以输入多个dicerolls,如果这需要帮助,请查看扫描仪。

答案 1 :(得分:0)

[
'http://www.marinetraffic.com/en/ais/details/ships/shipid:564352/imo:9643752/mmsi:511228000/vessel:DE%20MI',
'http://www.marinetraffic.com/en/ais/details/ships/shipid:3780155/imo:9712395/mmsi:477588800/vessel:SITC%20GUANGXI?cb=2267'
]

现在只需打印出变量即可完成!

我也可以使用Scanner in = new Scanner(System.in); Random gen = new Random(); int rollDice; int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0; System.out.println("How many die do you want to roll: "); int dice = in.nextInt(); while(dice > 0) { rolldice = gen.nextInt(6) + 1; //(1-6) if (rolldice == 1) { one++; } else if (rolldice == 2) { two++; } else if (rolldice == 3) { three++; } else if (rolldice == 4) { four++; } else if (rolldice == 5) { five++; } else if (rolldice == 6) { six++; } dice--; } 语句执行此操作,但我会让您弄明白如何转换,如果那就是您想要使用的内容。

相关问题