让计算机随机产生100个0到100之间的整数,每个整数代表某个学生的成绩,计算这些成绩平均值。
各位大侠帮我看看这个:
import java.util.*;
public class Test6 {
public static void main(String[] args){
Random R=new Random(101);
//int score=R.nextInt(101);这句放在不同位置,结果会不一样,这个为什么呢?
int sum=0;
for(int i=0;i<=100;i++){
//int score=R.nextInt(101);
sum=sum+score;
System.out.println("第"+(i+1)+"个同学的成绩为:"+score);
}
System.out.println("平均成绩为:"+(sum/100));
}
}
代码写错了。我把接收随机数的给写在for循环之外了。所以得出的100个同学的成绩都是一样的。只要把接收随机分数的代码放到for循环里面就可以了。不过还是要谢谢你。