附上问题: http: //puu.sh/42QtI/ea955e5bef.png
就代码而言,这是我目前所拥有的
这个问题要求“计算三个尾巴的模拟百分比”,这是我坚持的部分。有人能给我一些关于下一步进展的见解吗?
public static boolean isThreeTails(){
Random rand = new Random();
int numberOfTosses = 3;
int numberOfHeads = 0;
int numberOfTails = 0;
for(int i = 1; i <= numberOfTosses; i++){
int value = rand.nextInt(2);
if(value == 0){
numberOfTails++;
}else{
numberOfHeads++;
}
}
if(numberOfTails == 3){
return true;
}
else{
return false;
}
}
double numTosses = 1000000; //choose whatever here
double threeTails = 0;
for(int i =0; i < numTosses; i++){
if(isThreeTails()){
threeTails++;
}
}
System.out.println("Theoretical probability of 3 Tails: " + (double) 1/8);
System.out.println("Actual results for " + numTosses + " tosses = " + threeTails/numTosses);
编辑:在这里,我正在为有三尾时创建一个计数器。它会增加numberOfTripleTails
计数器。如果它滚动一个“H”,它numberOfTails
会简单地回到零。但是,我的代码似乎只给出“3”作为答案。
编辑2:完成!