GPA数组计算错误

时间:2016-04-20 01:35:47

标签: java

我的gpa程序存在问题,我必须使用两个数组来存储成绩,并且它的学分和计算gpa。到目前为止,其他一切似乎都在工作,除了gpa不能正确计算,我不确定我错过了什么(可能很简单)。

到目前为止我的代码是:

Gpa课程:

import java.util.*;

public class Gpa{

      int[] credits = new int[4];
      String[] grades = new String[4];

      private int numCourses;
      private int maxCourses;
      private int sumOfCourses;
      private int sumCredits;
      private int sumPoints;
      int newCredits;
      int totalSum = 0;
      int total = 0;


      public Gpa(int noCourses){
        maxCourses = noCourses;
        numCourses = 0; 


      }

      public void addCourse(int _newCredits, String newGrade){
         for (int i=0; i<maxCourses; i++){
           newCredits = _newCredits;
           credits[i] = newCredits;
         }
         for (int i=0; i<maxCourses; i++){
            grades[i] = newGrade;
         }
            switch (newGrade) {
               case "A":
               case "a":
                  newGrade = "4";
                  break;
               case "B":
               case "b":
                  newGrade = "3";
                  break;
               case "C":
               case "c":
                  newGrade = "2";
                  break;
               case "D":
               case "d":
                  newGrade = "1";
                  break;
               case "F":
               case "f":
                  newGrade = "0";
                  break;
              }
          sumPoints = sumPoints + (newCredits * Integer.parseInt(newGrade));
          numCourses++;

      }

      public double calcGPA(){
         for (int i=0; i<maxCourses; i++){
            sumCredits = sumCredits + newCredits;
         }
         double gpa = (double)sumPoints/sumCredits;
         return gpa;
      }


















} // end class

测试人员类:

import java.util.Scanner;

public class GpaTestEx2
{

    public static void main (String[] args)
    {
        //declarations
        Scanner in = new Scanner(System.in); //input object
        int numCourses; //number of courses - can be changed
        int credits;        //number of credits for a course
        String grade;       //grade for course


        //read in number of courses
        System.out.print("Enter number of courses: ");
        numCourses = in.nextInt();

        //create Gpa object to hold specified number of courses
        Gpa myGPA = new Gpa(numCourses);

        //read in all courses and add course information to Gpa object
        for (int k=0; k<numCourses; k++)
        {
            System.out.print("Enter credits for course " + (k+1) + ": ");
            credits = in.nextInt();
            System.out.print("Enter grade for course " + (k+1) + ": ");
            grade = in.next();

            myGPA.addCourse(credits, grade);
        }

        //print results
        System.out.println();
        System.out.printf("GPA is %4.2f%n", myGPA.calcGPA( )); 

    } //end main
}

当我输入课程学分和成绩时,它不能正确计算gpa。例如,如果用户输入说有2个课程,其中一个课程有4个学分,成绩为A,另一个课程有3个学分,成绩为B.我得到的学分为4.17,当它应该是3.57。

任何帮助都会很棒,我可能会或可能不会遗漏一些简单的东西。

1 个答案:

答案 0 :(得分:0)

每次有人插入值时,您似乎都在索引数组中的每个元素。我们只需要在添加新成绩时更改一个元素。 GPA课程:

public class Gpa {

    private int[] credits;
    private String[] grades;
    private int currentGrade;

    public Gpa(int numGrades) {
        credits = new int[numGrades];
        grades = new String[numGrades];
        currentGrade = 0;
    }

    public void addGrade(String letterGrade, int credit) {
        grades[currentGrade] = letterGrade;
        credits[currentGrade] = credit;
        currentGrade = currentGrade + 1;
    }

    public double getGpa() {
        double totalPoints = 0;
        double totalWeight = 0;

        for (int i = 0; i < currentGrade; i++) {
            totalPoints = totalPoints + (letterToGpa(grades[i]) * credits[i]);
            totalWeight = totalWeight + credits[i];
        }

        return totalPoints / totalWeight;
    }

    private double letterToGpa(String letter) {
        char first = letter.toUpperCase().charAt(0);
        switch (first) {
            case 'A':
                return 4.0;
            case 'B':
                return 3.0;
            case 'C':
                return 2.0;
            case 'D':
                return 1.0;
        }
        return 0.0;
    }
}

测试类现在应该正常工作:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in); //input object
    int numCourses; //number of courses - can be changed
    int credits;        //number of credits for a course
    String grade;       //grade for course

    //read in number of courses
    System.out.print("Enter number of courses: ");
    numCourses = in.nextInt();

    //create Gpa object to hold specified number of courses
    Gpa t = new Gpa(numCourses);

    //read in all courses and add course information to Gpa object
    for (int k=0; k<numCourses; k++)
    {
        System.out.print("Enter credits for course " + (k+1) + ": ");
        credits = in.nextInt();
        System.out.print("Enter grade for course " + (k+1) + ": ");
        grade = in.next();

        t.addGrade(grade, credits);
    }

    //print results
    System.out.println();
    System.out.printf("GPA is %4.2f%n", t.getGpa()); 
}

只是要注意,这显然不是最好的方法,也不是很好地遵循面向对象的模式,但OP的任务要求我们只使用一个类,等等。 / p>