如何获得正确的平均值和等级?

时间:2018-06-15 04:14:27

标签: java

这是我现在的代码:

package lessons4;
public class prog2 {

    public static void main(String args[])
    {
        int row , col, total=0;
        int average;
        int[][] y = {{85,83,77,91,76},
                    {80,90,95,93,48},
                    {78,81,11,90,73},
                    {92,83,30,69,87},
                    {23,45,96,38,59},
                    {60,85,45,39,67},
                    {77,31,52,74,83},
                    {93,94,89,77,97},
                    {79,85,28,93,82},
                    {85,72,49,75,63}
                    };

        String[] names = {"Johnson" , "Aniston" ,"Cooper", "Gupta", "Blair", "Clark", "Kennedy", "Bronson", "Sunny", "Smith"};

        System.out.println("Name \tS1 \tS2 \tS3 \tS4 \tS5 \taverage \tgrade ");
        for(row=0; row<=9; row++)
        {
            System.out.print(names[row] + "\t");
            for(col=0; col<=4; col++)
            {
                System.out.print(y[row][col] + "\t");
                total = total + y[row][col];
            }
            average = total/5;
            System.out.println(+average);
        }
    } 
}

这是我目前的输出:

Name    S1  S2  S3  S4  S5  average     grade 
Johnson 85  83  77  91  76  82
Aniston 80  90  95  93  48  163
Cooper  78  81  11  90  73  230
Gupta   92  83  30  69  87  302
Blair   23  45  96  38  59  354
Clark   60  85  45  39  67  413
Kennedy 77  31  52  74  83  477
Bronson 93  94  89  77  97  567
Sunny   79  85  28  93  82  640
Smith   85  72  49  75  63  709

如您所见,输出的平均值似乎不正确。

其次,我将如何为每个学生分配成绩,因为我对如何做到这一点感到很困惑。

1 个答案:

答案 0 :(得分:0)

您在正确的轨道上所需要做的就是将变量totalaverage的类型从 int 更改为 double ,因此平均值不会四舍五入到最接近的整数,并为每个学生重置total0,因此计算的totalaverage值特定于每个学生

至于分配字母等级,您可以使用下面getGrade之类的方法(根据需要进行修改):

class Prog2 {
  public static void main(String args[]) {
    int row, col = 0;
    double average, total = 0;
    int[][] y = {{85,83,77,91,76},
                {80,90,95,93,48},
                {78,81,11,90,73},
                {92,83,30,69,87},
                {23,45,96,38,59},
                {60,85,45,39,67},
                {77,31,52,74,83},
                {93,94,89,77,97},
                {79,85,28,93,82},
                {85,72,49,75,63}
                };

    String[] names = {"Johnson" , "Aniston" ,"Cooper", "Gupta", "Blair", "Clark", "Kennedy", "Bronson", "Sunny", "Smith"};

    System.out.println("Name \t\tS1 \tS2 \tS3 \tS4 \tS5 \tAverage \tGrade ");
    for(row=0; row<=9; row++) {
      System.out.print(names[row] + "\t\t");
      total = 0;
      for(col=0; col<=4; col++) {
        System.out.print(y[row][col] + "\t");
        total = total + y[row][col];
      }
      average = total/5;
      System.out.println(average + "\t\t" + getGrade(average));
    }
  }

  public static String getGrade(double average) {
    if(average > 97) {
      return "A+";
    } else if (average > 93) {
      return "A";
    } else if (average > 90) {
      return "A-";
    } else if (average > 87) {
      return "B+";
    } else if (average > 83) {
      return "B";
    } else if (average > 80) {
      return "B-";
    } else if (average > 77) {
      return "C+";
    } else if (average > 73) {
      return "C";
    } else if (average > 70) {
      return "C-";
    } else if (average > 67) {
      return "D+";
    } else if (average > 63) {
      return "D";
    } else if (average > 60) {
      return "D-";
    } else {
      return "F";
    }
  }
}

<强>输出:

Name        S1  S2  S3  S4  S5  Average     Grade 
Johnson     85  83  77  91  76  82.4        B-
Aniston     80  90  95  93  48  81.2        B-
Cooper      78  81  11  90  73  66.6        D
Gupta       92  83  30  69  87  72.2        C-
Blair       23  45  96  38  59  52.2        F
Clark       60  85  45  39  67  59.2        F
Kennedy     77  31  52  74  83  63.4        D
Bronson     93  94  89  77  97  90.0        B+
Sunny       79  85  28  93  82  73.4        C
Smith       85  72  49  75  63  68.8        D+