2014-01-27 113 views
0

我正在努力製作GPA計算器。我試圖解決的第一件事情是每個班級的個人GPA,這些GPA經過計算後會加總到總學分平均值。我定義了兩個變量(mathGPA和historyGPA),mathGPA工作正常,直到我添加了historyGPA。我現在擔心,隨着我在項目中的進展,先前編碼的個人GPA分數將不會被Java認可。代碼如下:爲什麼不mathGPA正常工作?

package problemset.leveltwo; 

import java.util.*; 

public class gpaCalculator { 

public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in); 

    // A+ 
    double a = 4.00; 

    // A- 
    double b = 3.70; 

    // B+ 
    double c = 3.30; 

    // B 
    double d = 3.00; 

    // B- 
    double e = 2.70; 

    // C+ 
    double f = 2.30; 

    // C 
    double g = 2.00; 

    // C- 
    double h = 1.70; 

    // D+ 
    double i = 1.30; 

    // D 
    double j = 1.00; 

    // D- 
    double k = 0.70; 

    // F 
    double l = 0; 

    System.out.println("Enter Your Math Grade (percentage)"); 
    int mathGrade = scanner.nextInt(); 

    System.out.println("Enter Your History Grade (percentage)"); 
    int historyGrade = scanner.nextInt(); 

    System.out.println("Enter Your English Grade (percentage)"); 
    int englishGrade = scanner.nextInt(); 

    System.out.println("Enter Your Science Grade (percentage)"); 
    int scienceGrade = scanner.nextInt(); 

    System.out.println("Enter Your Language Grade (percentage)"); 
    int languageGrade = scanner.nextInt(); 


    // MATH GRADE 

    // Math = A 
    if (mathGrade >= 96) { 
     System.out.println("Math GPA = " + a); 
     double mathGPA = 4.00; 
     } 

    // Math = A- 
    if (mathGrade < 96) { 
     if (mathGrade >= 90) { 
      System.out.println("Math GPA = " + b); 
      double mathGPA = 3.70; 
     } 
    } 

    // Math B+ 
    if (mathGrade < 90) { 
     if (mathGrade >= 86) { 
      System.out.println("Math GPA = " + c); 
      double mathGPA = 3.30; 
     } 
    } 

    // Math B 
    if (mathGrade < 86) { 
     if (mathGrade >= 84) { 
      System.out.println("Math GPA = " + d); 
      double mathGPA = 3.00; 
     } 
    } 

    // Math B- 
    if (mathGrade < 84) { 
     if (mathGrade >= 80) { 
      System.out.println("Math GPA = " + e); 
      double mathGPA = 2.70; 
     } 
    } 

    // Math C+ 
    if (mathGrade < 80) { 
     if (mathGrade >= 76) { 
      System.out.println("Math GPA = " + f); 
      double mathGPA = 2.30; 
     } 
    } 

    // Math C 
    if (mathGrade < 76) { 
     if (mathGrade >= 73) { 
      System.out.println("Math GPA = " + g); 
      double mathGPA = 2.00; 
     } 
    } 

    // Math C- 
    if (mathGrade < 73) { 
     if (mathGrade >= 70) { 
      System.out.println("Math GPA = " + h); 
      double mathGPA = 1.70; 
     } 
    } 

    // Math D+ 
    if (mathGrade < 70) { 
     if (mathGrade >= 66) { 
      System.out.println("Math GPA = " + i); 
      double mathGPA = 1.30; 
     } 
    } 

    // Math D 
    if (mathGrade < 66) { 
     if (mathGrade >= 63) { 
      System.out.println("Math GPA = " + j); 
      double mathGPA = 1.00; 
     } 
    } 

    // Math D- 
    if (mathGrade < 63) { 
     if (mathGrade >= 50) { 
      System.out.println("Math GPA = " + k); 
      double mathGPA = 0.70; 
     } 
    } 

    // Math F 
    if (mathGrade < 50) { 
     System.out.println("Math GPA = " + 0); 
     double mathGPA = 0.00; 
    } 



    // HISTORY GRADE 

    // History = A 
    if (historyGrade >= 96) { 
     System.out.println("History GPA = " + a); 
     double historyGPA = 4.00; 
     } 

    // History = A- 
    if (historyGrade < 96) { 
     if (historyGrade >= 90) { 
      System.out.println("History GPA = " + b); 
      double historyGPA = 3.70; 
     } 
    } 

    // History B+ 
    if (historyGrade < 90) { 
     if (historyGrade >= 86) { 
      System.out.println("History GPA = " + c); 
      double historyGPA = 3.30; 
     } 
    } 

    // History B 
    if (historyGrade < 86) { 
     if (historyGrade >= 84) { 
      System.out.println("History GPA = " + d); 
      double historyGPA = 3.00; 
     } 
    } 

    // History B- 
    if (historyGrade < 84) { 
     if (historyGrade >= 80) { 
      System.out.println("History GPA = " + e); 
      double historyGPA = 2.70; 
     } 
    } 

    // History C+ 
    if (historyGrade < 80) { 
     if (historyGrade >= 76) { 
      System.out.println("History GPA = " + f); 
      double historyGPA = 2.30; 
     } 
    } 

    // History C 
    if (historyGrade < 76) { 
     if (historyGrade >= 73) { 
      System.out.println("History GPA = " + g); 
      double historyGPA = 2.00; 
     } 
    } 

    // History C- 
    if (historyGrade < 73) { 
     if (historyGrade >= 70) { 
      System.out.println("History GPA = " + h); 
      double historyGPA = 1.70; 
     } 
    } 

    // History D+ 
    if (historyGrade < 70) { 
     if (historyGrade >= 66) { 
      System.out.println("History GPA = " + i); 
      double historyGPA = 1.30; 
     } 
    } 

    // History D 
    if (historyGrade < 66) { 
     if (historyGrade >= 63) { 
      System.out.println("History GPA = " + j); 
      double historyGPA = 1.00; 
     } 
    } 

    // History D- 
    if (historyGrade < 63) { 
     if (historyGrade >= 50) { 
      System.out.println("History GPA = " + k); 
      double historyGPA = 0.70; 
     } 
    } 

    // History F 
    if (historyGrade < 50) { 
     System.out.println("History GPA = " + 0); 
     double historyGPA = 0; 


double total = mathGPA + historyGPA; 
System.out.println("Total GPA = " + total); 


     } 

     } 

} 

錯誤消息Java Eclipse給我的是「mathGPA無法解析爲變量」。 我不知道我在寫什麼不正確,但我希望有人能給我一些見解來糾正我的代碼。

+0

您可以在'if'塊中定義'mathGPA'變量。他們非常地方化,在這些街區之外不可見。在'Scanner'行之後放置'double mathGPA',並在您使用它的任何地方移除'double'。 –

+0

那麼我應該如何根據學生輸入的年級來更改它? – user3238852

+0

@ PM77-1那麼如何根據用戶輸入更改mathGPA? – user3238852

回答

2

正如您指出的那樣,在if語句中定義了mathgpa,所以它只存在於那裏。

在if語句之前爲它定義一個double並擺脫冗餘。

下學習使用if..then..else讓生活更美好

如果你看看本教程中的最後一個例子是shoukd幫助

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

基本上做到了,如果再如果不匹配,則使用else,直到找到匹配的最終else

if (grade > 90) { 
    // A 
} else if (grade > 80) { 
    // B 
} else { 
    // F 
} 

因此,如果B小於90且大於8 0且任何小於80的值都是F.