[英]Dice Roll Simulation
所以我迷路了。 这是目标:
问多少次掷一个六面骰子。 随机掷骰子。 打印出每个数字在1到6之间出现了多少次
这个循环事情是否正确? 我应该从什么开始我真的很困惑
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++;
}
}
我假设骰子整数是用户的输入。 如果每次迭代都减少骰子,则将骰子滚动(遍历循环),次数取决于用户要求。
不知道您是否已实现它,以便用户可以输入许多骰子,如果您需要帮助,请参阅“扫描器”。
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--;
}
现在只需打印出变量即可完成操作!
我也可以使用switch
语句来做到这一点,但是我将让您弄清楚如果要使用的内容该如何进行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.