![](/img/trans.png)
[英]How do I simulate 3000 dice rolls and count the number of times doubles are rolled? JAVA
[英]for-loop controlling the number of times the Dice are rolled. (Java)
我正在制作一个 Java 程序,以允许用户输入掷骰子的次数。 我正在使用带有 timesRolled 变量的 for 循环来控制循环程序的次数。 我想添加所有随机骰子值以获得总和。
int timesRolled = 4;
int result = 0;
for (int i = 0; i < timesRolled; i++) {
int rand1 = getRandom(1,6); // getrandom is a function withing the template.
result = rand1 ;
outputln(result);
}
您需要在每次迭代中将 result 的当前值添加到新确定的 dice 值,并将其分配给结果: result = result + rand1
或result += rand1
简称。 也调用outputln(result);
在 for 循环中,每次迭代都会输出累积和,因此您可能希望将其移出最终仅输出一次。
int timesRolled = 4;
int result = 0;
for (int i = 0; i < timesRolled; i++) {
int rand1 = getRandom(1,6); // getrandom is a function withing the template.
result += rand1 ;
}
outputln(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.