2015-08-28 36 views
-1
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個陣列credits[]grades[],其中第一個是int,第二個是String。我需要使用方法addCourse(需要兩個參數:newCredits(課程學分數)和newGrade - 課程成績爲String(僅限一個字母))。沒有返回值,因爲假設數組中有空間可以添加課程。 addCourse info:在信用數組的第一個可用位置存儲newCredits用數組計算gpa(字符串到int運行錯誤)

將newGrade存儲在等級數組中的相同位置。 遞增numCourses以指示用戶已經輸入了一個課程。

calcGpa(無參數,返回GPA爲雙) - 計算並返回基於A = 4分標準GPA,B = 3,C = 2,d = 1,F = 0。如果無法計算GPA,則返回0.0。

構造函數:noCourses - 最大課程數。沒有返回類型。

更多信息:爲參數大小的兩個數組分配空間。設置maxCourses到noCourses.Sets的numCourses零

我的代碼:GPA類需要返工:

import java.util.*; 
public class Gpa 
{ 
    Scanner in = new Scanner(System.in); 
    int numCourses; 
    int maxCourses; 
    int [ ] credits = new int [maxCourses]; 
    String [ ] grades = new String [maxCourses]; 
    int total = 0; 
    int totalSum = 0; 
    public Gpa(int noCourses) 
    { 
     maxCourses = noCourses; 
     numCourses = 0; 
    } 
    public void addCourse(int newCredits, String newGrade) 
    { 

     for(int i = 0; i < numCourses; i++) 
     { 
     credits[i] = newCredits; 
     } 
     for(int i = 0; i < numCourses; i++) 
     { 
     grades[i] = newGrade; 
     } 
     switch(newGrade) 
     { 
     case "A": newGrade = 4; 
      break; 
     case "a": newGrade = 4; 
      break; 
     case "B": newGrade = 3; 
      break; 
     case "b": newGrade = 3; 
      break; 
     case "C": newGrade = 2; 
      break; 
     case "c": newGrade = 2; 
      break; 
     case "D": newGrade = 1; 
      break; 
     case "d": newGrade = 1; 
      break; 
     case "F": newGrade = 0; 
      break; 
     case "f": newGrade = 0; 
      break;  
     } 
     totalSum = totalSum + (newCredits * newGrade); 


     numCourses++; 
    } 
    public double calcGPA() 
    { 
     for (int x : credits) 
     { 
     total+=x; 
     } 

     double gpa = (double)totalSum/total; 
     return gpa; 
    } 

} 

我得到詮釋的錯誤不能被轉換成字符串。而且我需要將這個新的等級從(A,B,C,D,F ..)等級的字符串轉換爲等級所代表的信用數字。它在這種情況下工作:在我的第一部分與char,我試圖看看我的第一部分,它工作正常,我注意到,字符更容易轉換爲int,基本上我不需要轉換它但String,它doesn以這種方式工作,我試圖谷歌它,但我找不到合適的解決方案。我需要在這部分中迷失方向的指導。這是工作的一部分:第1部分:

import java.util.*; 
public class GpaEx1 
{ 
    public static final int NUMBER_OF_COURSES = 4; 
    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     int [ ] credits = new int [NUMBER_OF_COURSES]; 
     char [ ] grades = new char [NUMBER_OF_COURSES]; 

     int total = 0; 
     int totalSum = 0; 

     for(int i = 0; i < NUMBER_OF_COURSES; i++) 
     { 
     System.out.println("Enter credits for course " + (i+1) + ": "); 
     int credit = in.nextInt(); 
     credits[i] = credit; 

     System.out.println("Enter grade for course " + (i+1) + ": "); 
     char grade = in.next().charAt(0); 
     grades[i] = grade; 

     switch(grade) 
     { 
      case 'A': grade = 4; 
       break; 
      case 'a': grade = 4; 
       break; 
      case 'B': grade = 3; 
       break; 
      case 'b': grade = 3; 
       break; 
      case 'C': grade = 2; 
       break; 
      case 'c': grade = 2; 
       break; 
      case 'D': grade = 1; 
       break; 
      case 'd': grade = 1; 
       break; 
      case 'F': grade = 0; 
       break; 
      case 'f': grade = 0; 
       break; 
     } 
     totalSum = totalSum + (credit * grade); 

     } 
     for (int x : credits) 
     { 
     total+=x; 
     } 

     System.out.println("Total number of credits: " + total); 
     System.out.println("Total number of points: " + totalSum); 
     double gpa = (double)totalSum/total; 
     System.out.printf("Gpa: %.2f", gpa); 

    } 
} 
+0

哪條線給你錯誤,那是什麼錯誤? –

+0

你用一個char乘以一個int。您需要將等級設爲數值數據類型 – Turtle

+0

基本上所有開關(newGrade)都會給我提供錯誤,尤其是在newGrade = 4時;或任何數字它說int不能轉換爲字符串:totalSum = totalSum +(newCredits * newGrade); ^ 第一種類型:int 第二種類型:字符串 11錯誤 – OneTwoThree

回答

0

不能在功能public void addCourse(int newCredits, String newGrade),特別是在switch情況下newGrade分配String variableInteger值。因此,switch案例中的分配行需要在""引號內。

像這樣:newGrade = "4"

此外,您在功能public void addCourse(int newCredits, String newGrade),特別是在該行totalSum = totalSum + (newCredits * newGrade)

基本上得到一個再次另一個錯誤,你不能乘一個Interger數據類型,是newCreditsString數據類型是newGrade。要將字符串newGrade的值解析爲Integer值,您可以使用在Java中內置的String.valueOf(number)Integer.toString(number)函數。

爲您的特定情況下,你可以使用totalSum = totalSum + (newCredits * String.valueOf(newGrade))

這將解決編譯器錯誤,讓我知道如果有更多的錯誤來。

+0

好先生機器人我固定第一個和第二個我用:totalSum = totalSum +(newCredits * String.valueOf(newGrade)),我得到這個:Gpa。 java:50:錯誤:二進制運算符'*'的操作數類型錯誤*' totalSum = totalSum +(newCredits * String.valueOf(newGrade)); ^ 第一種:int 第二種類型:字符串 1錯誤 – OneTwoThree

+0

totalSum = totalSum +(newCredits * Integer.parseInt(newGrade));我把這個這似乎工作,但我得到這個錯誤在這裏:credits [i] = newCredits;在線程「main」中聲明異常java.lang.ArrayIndexOutOfBoundsException:0 – OneTwoThree

+0

這是因爲在你的for循環條件中,你一直保持我的