我一直试图在这段代码中找到错误的位置已经有一段时间了,我无法弄明白.该程序将6面模具滚动300次,然后输出每个数字滚动的时间.但出于某种原因,它不是滚动300次,而是滚动320次.我没有看到for循环有什么问题,所以我真的不知所措.
public static void dieRoll(){
int[] roll = new int [300];
int[] count = new int[] {1,2,3,4,5,6};
for(int i = 1; i<300; i++){
roll[i] = (int) Math.ceil( (int) (Math.random()*6)+1 );
// roll[i] = (int) Math.ceil(roll[i]);
// System.out.println(roll[i]);
if(roll[i]==1){
count[0]++;
}
else if(roll[i]==2){
count[1]++;
}
else if(roll[i]==3){
count[2]++;
}
else if(roll[i]==4){
count[3]++;
}
else if(roll[i]==5){
count[4]++;
}
else if(roll[i]==6){
count[5]++;
}
// System.out.println(roll[i]);
}//i loop
System.out.println("The die landed on 1 " + count[0] + " times.");
System.out.println("The die landed on 2 …Run Code Online (Sandbox Code Playgroud)