为什么mathGPA不能正常工作?

时间:2014-01-27 01:33:42

标签: java javascript eclipse math calculator

我正在制作GPA计算器。我试图解决的第一件事就是每个班级的个人GPA,经过计算,将累加到平均成绩点。我定义了两个变量(mathGPA和historyGPA)mathGPA工作正常,直到我添加了historyGPA。我现在担心,随着项目的进展,以前编码的个人GPA分数将无法被Java识别。这是代码:

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无法解析为变量”。 我不知道我写错了什么,但我希望有人能给我一些洞察力来纠正我的代码。

1 个答案:

答案 0 :(得分:2)

正如所指出的,你在if语句中定义了mathgpa,所以它只存在于那里。

在if语句之前为它定义一个double并摆脱冗余。

接下来学会使用if..then..else让生活更美好

如果您查看本教程中的最后一个示例,请提示帮助

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

基本上做一个if if if then then if else else if if到达最后的其他匹配

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

因此,如果B小于90且大于80且小于80的任何一个是F。