2016-04-20 14 views
0

私はグレードを保存するために2つの配列を使用する必要があり、それがクレジットだとGPAを計算ところ、私は私の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を計算しません。 。例えば、ユーザ入力はA、それは3.57であるべきときに私は周りの4.17のGPAを取得するBのグレードを持つ他の持つ3つのクレジットのグレードで4つの単位を持つ一つのクラスとの2コースがあると言います。

すべてのヘルプは素晴らしいことだ、私は、または単純な何かが欠けていてもいなくてもよいです。

+0

なぜ、 'addCourse'では、配列のすべての要素を新しく入力した等級に設定していますか?おそらく、私はロジックに何かが見当たりませんが、配列の最新のエントリだけを設定したいと思うようです。また、Stringに設定してから値を解析するのではなく、ポイントとしてローカル変数をintとして使用します。 – KevinO

答えて

0

あなたは、配列内のすべての要素の中に誰かが値を挿入するたびにインデックス化されているように思えます。新しいグレードが追加されたときに要素を1つだけ変更する必要があります。 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の割り当てでは、1つのクラスしか使用しないようにする必要があります。

+0

残念ながら私はGPAクラスとテスターでそれを保つことになっています – BrianAndris

+0

@BrianAndrisこれは問題ありません。簡単に組み合わせることができます。それを表示するために私の答えを編集する必要がありますか? – nhouser9

+0

あなたが気にしていない場合は、その間に病気を見て、私ができることを見てみてください。 – BrianAndris