2017-03-17 121 views
0

嗨,我想知道我是否可以得到一些GPA計算器的幫助。GPA計算器幫助

它所需要做的是:

  1. 的投入將包括術語的序列,例如,學期。

  2. 每學期的投入將包括該學期內所選課程的成績和學分。

  3. 對於每個術語,用戶將輸入一個整數,該整數表示在該術語內所採用的課程數 。
  4. 每個課程由一個字符串字母等級和一個整數個學分指定,順序以空格分隔。 5.如果用戶輸入-1來表示學期中所選課程的數量,那麼程序必須打印最終的總體摘要,然後終止。
  5. 不要提示輸入任何信息。因此,在BlueJ中運行程序後,請鍵入Ctrl-T以強制終端窗口彈出。
  6. 一如既往,請遵循「樣品運行」部分中描述的輸入/輸出格式。

下面顯示的是錯誤消息我得到和我的代碼,感謝您對提前或提示我可以嘗試任何援助。

終端窗口和錯誤消息: Terminal

import java.util.Scanner; 
/* 
* 
* 
*/ 
public class Prog2 { 

    public static void main(String args[]) { 

    Scanner numberInput = new Scanner(System.in); 
    int numberofClasses = numberInput.nextInt(); 
    Scanner input = new Scanner(System.in); 
    String [] grade = new String[5]; 
    int [] credit = new int [5]; 
    double totalCredit = 0.0; 
    double realGrade = 0.0; 
    double result = 0.0; 

    while (numberofClasses > 0) 
    { 
     for (int x = 0; x < numberofClasses; x++) 
     { 
      grade[x] = input.next(); 

      credit[x] = input.nextInt(); 

     } 
     for(int x=0;x < numberofClasses; x++){ 
      if(grade[x].equals("A+")){ 
       realGrade=4.0;    
      } 
      else if(grade[x].equals("A")){ 
       realGrade=4.0; 
      } 
      else if(grade[x].equals("A-")){ 
       realGrade=3.67;   
      } 
      else if(grade[x].equals("B+")){ 
       realGrade=3.33;   
      } 
      else if(grade[x].equals("B")){ 
       realGrade=3.00;   
      } 
      else if(grade[x].equals("B-")){ 
       realGrade=2.67;   
      } 
      else if(grade[x].equals("C+")){ 
       realGrade=2.33;   
      } 
      else if(grade[x].equals("C")){ 
       realGrade=2.00;   
      } 
      else if(grade[x].equals("C-")){ 
       realGrade=1.33;   
      } 

      result = result+realGrade*credit[x]; 
      totalCredit=totalCredit+credit[x]; 
     } 
     System.out.println("Summary for term:"); 
     System.out.println("----------------------------------"); 
     System.out.println("Term total grade points: " + result); 
     System.out.println("Term total credits:" + totalCredit); 
     System.out.println("GPA:"+result/totalCredit); 


    } 
     // This block is getting used later please ignore 
     System.out.println("Final Summary:"); 
     System.out.println("----------------------------------"); 
     System.out.println(" Overall terms"); 
     System.out.println(" Total grade points: " + result);// this needs to be all); 
     System.out.println(" Total credits" + totalCredit);//This needs to be all); 
     System.out.println("Cumulative GPA:"+result/totalCredit); 

    } 
} 
+0

PS:有些化妝品你的運動:你有一定的空間,以多你的println總結。總是沒有前導空間'(「Term」或帶前導空格'(「Total」。並且總是在':「後面或後面加空格':」' – muescha

回答

0

當while循環結束,numberofClasses仍然包含了while循環開始了第一次之前所輸入的值。具體來說,你輸出的行後:

GPA=3.0588... 

你打的循環結束,然後返回到:

while (numberofClasses > 0) 

這是真的。你進入不進入numberofClasses接下來的「3」,它是由

grade[x] = input.next(); 

那麼「A」是由

credit[x] = input.nextInt(); 

拿起它拋出一個異常,拿起因爲它不是一個整數。

所有你需要做的是在while循環結束時再次要求的班數:

System.out.println("GPA:"+result/totalCredit); 
    numberofClasses = numberInput.nextInt(); 
} 

輸出:

5 
A 3 
B 2 
C 4 
A 5 
C 3 
Summary for term: 
---------------------------------- 
Term total grade points: 52.0 
Term total credits:17.0 
GPA:3.0588235294117645 
3 
A 3 
B 5 
C 1 
Summary for term: 
---------------------------------- 
Term total grade points: 81.0 
Term total credits:26.0 
GPA:3.1153846153846154 
+0

我還有一個問題,如果你不介意幫助一點點更多當我再次通過循環並打印出GPA,信用等等......有沒有辦法我只能打印出該循環中使用的數據,而不是輸入累計總數? – UnluckySurvivor

+0

重置變量當您進入下一個循環時,將其設爲零 – muescha

+0

謝謝您的幫助! – UnluckySurvivor

0

我建議考慮是否你的編譯器或IDE具有「調試」功能。這是一個非常有用的工具,並允許您監視您的程序如何去通過您的代碼

只是一個提示...

當你要求輸入,打印你的要求第一。當我啓動你的程序時,我不知道該怎麼做。嘗試添加System.out.println("input number of classes you took");您提示該號碼之前。

這裏是什麼是錯的。 (如果你打印出你要求的第一個,這會更明顯)。

程序後顯示的統計數據,你進入5然而,你的程序實際上仍然在第22行此行grade[x] = input.next();我相信。

當你進入5

,您的掃描儀在等信。並拋出異常。

,你需要考慮如何逃離這裏這個循環。 while (numberofClasses > 0)也許使用if語句?否則你的程序循環爲永遠,永遠不會要求一個新的類號

+0

該程序的參數專門用於不提示用戶,否則我會。 – UnluckySurvivor

+0

謝謝你的幫助! – UnluckySurvivor