【发布时间】:2020-07-12 19:49:43
【问题描述】:
我正在使用一个随机数类并要求它将总正面数/翻转次数相加....但它不起作用。
任何提示都会有所帮助。
import java.util.Random;
public class FlipaCoin
{
public static void main(String [] args)
{
Random rand= new Random();
boolean a;
while (a=true)
{
int flip=rand.nextInt(2);
int heads=0;
if (flip==1)
{
heads++;
}
double count=0;
double percentage=heads/count;
System.out.println(percentage);
count++;
if (percentage==50.0)
{
break;
}
}
}
}
【问题讨论】:
-
声明并初始化 heads 变量 above while 循环(不在其中)。就像现在一样,while 循环的每次迭代都会将 heads 变量设为 0。对 count 执行相同操作。
标签: java for-loop random logic counter