Java测试成绩簿

时间:2014-04-25 16:48:20

标签: java

我目前正在尝试创建一个java应用程序,该应用程序创建一个数组,用于存储0到100之间随机数的20个测试等级。然后我想计算/打印最高/平均分数。这是我到目前为止,但我一直在收到错误。有什么帮助吗?

  import java.util.Random;

    public class ComputerGrades 
    {
         public static void main(String[] args)
         {

            Random r = new Random();
            int total = 0;


             int[] studentGrades = new int[20]; 


             for ( int i=0; i<20; i++ )
             {
                studentGrades[i] = r.NextInt();

                 System.out.printf("%d", studentGrades[i]);

                total+= studentGrades[i];

             }
            int max = studentGrades[0];
            for ( int i=1; i<20; i++ )
             {
                if(studentGrades[i] > max)
                    max=studentGrades[i];
            }


    System.out.printf("\nThe average is %d", total/20);
    System.out.printf("\nThe highest grades is %d", max);

         }
    }

2 个答案:

答案 0 :(得分:2)

当您致电nextInt时,请提供最多101个(因此结果为0,最多不包括):

studentGrades[i] = r.nextInt(101); // will be a number from 1 - 100

此外,除以数组长度,而不是“幻数”。如果你想更进一步,平均使用双精度小数:

System.out.printf("\nThe average is %.2f", (double) total/(double) studentGrades.length);

答案 1 :(得分:0)

final int SIZE = 20;
int[] grades = new int[SIZE];

for (int i = 0; i < SIZE; i++) grades[i] = Math.random() * 101;

然后找到数组成绩元素的最高和平均分数。

相关问题