等级点平均计算器

时间:2014-12-26 12:02:16

标签: java

我需要帮助这是java中g.p.a计算器的代码,但它一直给我错误的变量未初始化...我不知道问题是什么。 (源代码:http://pastebin.com/m6q0zpD4

import javax.swing.JOptionPane;

public class coursework_1 {

public static void main(String args[]) {
    String Course1;
    String Course2;
    String Course3;
    String Course4;
    String Course5;
    String Course6;
    String Course7;
    String Course8;
    String Course9;
    String courseUnit1, courseUnit2, courseUnit3, courseUnit4, courseUnit5, courseUnit6, courseUnit7, courseUnit8, courseUnit9;
    int c1, c2, c3, c4, c5, c6, c7, c8, c9;
    int creditUnit1, creditUnit2, creditUnit3, creditUnit4, creditUnit5, creditUnit6, creditUnit7, creditUnit8, creditUnit9;
    int Course1Val, Course2Val, Course3Val, Course4Val, Course5Val, Course6Val, Course7Val, Course8Val, Course9Val;
    String cG1, cG2, cG3, cG4, cG5, cG6, cG7, cG8, cG9;
    char A = 'A';
    char B = 'B';
    char C = 'C';
    char D = 'D';
    char E = 'E';
    char F = 'F';
    int totalCourseVal;
    int totalCreditUnit;
    int gpa;
    Course1 = JOptionPane.showInputDialog("Enter your total score for Course1 ");
    courseUnit1 = JOptionPane.showInputDialog("Enter credit unit of first course");
    Course2 = JOptionPane.showInputDialog("Enter your total score for Course2 ");
    courseUnit2 = JOptionPane.showInputDialog("Enter credit unit of second course");
    Course3 = JOptionPane.showInputDialog("Enter your total score for Course3 ");
    courseUnit3 = JOptionPane.showInputDialog("Enter credit unit of third course");
    Course4 = JOptionPane.showInputDialog("Enter your total score for Course4 ");
    courseUnit4 = JOptionPane.showInputDialog("Enter credit unit of fourth course");
    Course5 = JOptionPane.showInputDialog("Enter your total score for Course5 ");
    courseUnit5 = JOptionPane.showInputDialog("Enter credit unit of fifth course");
    Course6 = JOptionPane.showInputDialog("Enter your total score for Course6 ");
    courseUnit6 = JOptionPane.showInputDialog("Enter credit unit of sixth course");
    Course7 = JOptionPane.showInputDialog("Enter your total score for Course7 ");
    courseUnit7 = JOptionPane.showInputDialog("Enter credit unit of seventh course");
    Course8 = JOptionPane.showInputDialog("Enter your total score for Course8 ");
    courseUnit8 = JOptionPane.showInputDialog("Enter credit unit of eigth course");
    Course9 = JOptionPane.showInputDialog("Enter your total score for Course9 ");
    courseUnit9 = JOptionPane.showInputDialog("Enter credit unit of nineth course");
    c1 = Integer.parseInt(Course1);
    creditUnit1 = Integer.parseInt(courseUnit1);
    c2 = Integer.parseInt(Course2);
    creditUnit2 = Integer.parseInt(courseUnit2);
    c3 = Integer.parseInt(Course3);
    creditUnit3 = Integer.parseInt(courseUnit3);
    c4 = Integer.parseInt(Course4);
    creditUnit4 = Integer.parseInt(courseUnit4);
    c5 = Integer.parseInt(Course5);
    creditUnit5 = Integer.parseInt(courseUnit5);
    c6 = Integer.parseInt(Course6);
    creditUnit6 = Integer.parseInt(courseUnit6);
    c7 = Integer.parseInt(Course7);
    creditUnit7 = Integer.parseInt(courseUnit7);
    c8 = Integer.parseInt(Course8);
    creditUnit8 = Integer.parseInt(courseUnit8);
    c9 = Integer.parseInt(Course9);
    creditUnit9 = Integer.parseInt(courseUnit9);
    if (c1 >= 70) {
        cG1 = "A";
    } else if (c1 >= 60) {
        cG1 = "B";
    } else if (c1 >= 50) {
        cG1 = "C";
    } else if (c1 >= 45) {
        cG1 = "D";
    } else if (c1 >= 40) {
        cG1 = "E";
    } else if (c1 < 40) {
        cG1 = "F";
    }
    if (c2 >= 70) {
        cG2 = "A";
    } else if (c2 >= 60) {
        cG2 = "B";
    } else if (c2 >= 50) {
        cG2 = "C";
    } else if (c2 >= 45) {
        cG2 = "D";
    } else if (c2 >= 40) {
        cG2 = "E";
    } else if (c2 < 40) {
        cG2 = "F";
    }
    if (c3 >= 70) {
        cG3 = "A";
    } else if (c3 >= 60) {
        cG3 = "B";
    } else if (c3 >= 50) {
        cG3 = "C";
    } else if (c3 >= 45) {
        cG3 = "D";
    } else if (c3 >= 40) {
        cG3 = "E";
    } else if (c3 < 40) {
        cG3 = "F";
    }
    if (c4 >= 70) {
        cG4 = "A";
    } else if (c4 >= 60) {
        cG4 = "B";
    } else if (c4 >= 50) {
        cG4 = "C";
    } else if (c4 >= 45) {
        cG4 = "D";
    } else if (c4 >= 40) {
        cG4 = "E";
    } else if (c4 < 40) {
        cG4 = "F";
    }
    if (c5 >= 70) {
        cG5 = "A";
    } else if (c5 >= 60) {
        cG5 = "B";
    } else if (c5 >= 50) {
        cG5 = "C";
    } else if (c5 >= 45) {
        cG5 = "D";
    } else if (c5 >= 40) {
        cG5 = "E";
    } else if (c5 < 40) {
        cG5 = "F";
    }
    if (c6 >= 70) {
        cG6 = "A";
    } else if (c6 >= 60) {
        cG6 = "B";
    } else if (c6 >= 50) {
        cG6 = "C";
    } else if (c6 >= 45) {
        cG6 = "D";
    } else if (c6 >= 40) {
        cG6 = "E";
    } else if (c6 < 40) {
        cG6 = "F";
    }
    if (c7 >= 70) {
        cG7 = "A";
    } else if (c7 >= 60) {
        cG7 = "B";
    } else if (c7 >= 50) {
        cG7 = "C";
    } else if (c7 >= 45) {
        cG7 = "D";
    } else if (c7 >= 40) {
        cG7 = "E";
    } else if (c7 < 40) {
        cG7 = "F";
    }
    if (c8 >= 70) {
        cG8 = "A";
    } else if (c8 >= 60) {
        cG8 = "B";
    } else if (c8 >= 50) {
        cG8 = "C";
    } else if (c8 >= 45) {
        cG8 = "D";
    } else if (c8 >= 40) {
        cG8 = "E";
    } else if (c8 < 40) {
        cG8 = "F";
    }
    if (c9 >= 70) {
        cG9 = "A";
    } else if (c9 >= 60) {
        cG9 = "B";
    } else if (c9 >= 50) {
        cG9 = "C";
    } else if (c9 >= 45) {
        cG9 = "D";
    } else if (c9 >= 40) {
        cG9 = "E";
    } else if (c9 < 40) {
        cG9 = "F";
    }
    if (cG1 == "A") {
        Course1Val = 5 * creditUnit1;
    } else if (cG1 == "B") {
        Course1Val = 4 * creditUnit1;
    } else if (cG1 == "C") {
        Course1Val = 3 * creditUnit1;
    } else if (cG1 == "D") {
        Course1Val = 2 * creditUnit1;
    } else if (cG1 == "E") {
        Course1Val = 1 * creditUnit1;
    } else if (cG1 == "F") {
        Course1Val = 0 * creditUnit1;
    }
    if (cG2 == "A") {
        Course2Val = 5 * creditUnit2;
    } else if (cG2 == "B") {
        Course2Val = 4 * creditUnit2;
    } else if (cG2 == "C") {
        Course2Val = 3 * creditUnit2;
    } else if (cG2 == "D") {
        Course2Val = 2 * creditUnit2;
    } else if (cG2 == "E") {
        Course2Val = 1 * creditUnit2;
    } else if (cG2 == "F") {
        Course2Val = 0 * creditUnit2;
    }
    if (cG3 == "A") {
        Course3Val = 5 * creditUnit3;
    } else if (cG3 == "B") {
        Course3Val = 4 * creditUnit3;
    } else if (cG3 == "C") {
        Course3Val = 3 * creditUnit3;
    } else if (cG3 == "D") {
        Course3Val = 2 * creditUnit3;
    } else if (cG3 == "E") {
        Course3Val = 1 * creditUnit3;
    } else if (cG3 == "F") {
        Course3Val = 0 * creditUnit3;
    }
    if (cG4 == "A") {
        Course4Val = 5 * creditUnit4;
    } else if (cG4 == "B") {
        Course4Val = 4 * creditUnit4;
    } else if (cG4 == "C") {
        Course4Val = 3 * creditUnit4;
    } else if (cG4 == "D") {
        Course4Val = 2 * creditUnit4;
    } else if (cG4 == "E") {
        Course4Val = 1 * creditUnit4;
    } else if (cG4 == "F") {
        Course4Val = 0 * creditUnit4;
    }
    if (cG5 == "A") {
        Course5Val = 5 * creditUnit5;
    } else if (cG5 == "B") {
        Course5Val = 4 * creditUnit5;
    } else if (cG5 == "C") {
        Course5Val = 3 * creditUnit5;
    } else if (cG5 == "D") {
        Course5Val = 2 * creditUnit5;
    } else if (cG5 == "E") {
        Course5Val = 1 * creditUnit5;
    } else if (cG5 == "F") {
        Course5Val = 0 * creditUnit5;
    }
    if (cG6 == "A") {
        Course6Val = 5 * creditUnit6;
    } else if (cG6 == "B") {
        Course6Val = 4 * creditUnit6;
    } else if (cG6 == "C") {
        Course6Val = 3 * creditUnit6;
    } else if (cG6 == "D") {
        Course6Val = 2 * creditUnit6;
    } else if (cG6 == "E") {
        Course6Val = 1 * creditUnit6;
    } else if (cG6 == "F") {
        Course6Val = 0 * creditUnit6;
    }
    if (cG7 == "A") {
        Course7Val = 5 * creditUnit7;
    } else if (cG7 == "B") {
        Course7Val = 4 * creditUnit7;
    } else if (cG7 == "C") {
        Course7Val = 3 * creditUnit7;
    } else if (cG7 == "D") {
        Course7Val = 2 * creditUnit7;
    } else if (cG7 == "E") {
        Course7Val = 1 * creditUnit7;
    } else if (cG7 == "F") {
        Course7Val = 0 * creditUnit7;
    }
    if (cG8 == "A") {
        Course8Val = 5 * creditUnit8;
    } else if (cG8 == "B") {
        Course8Val = 4 * creditUnit8;
    } else if (cG8 == "C") {
        Course8Val = 3 * creditUnit8;
    } else if (cG8 == "D") {
        Course8Val = 2 * creditUnit8;
    } else if (cG8 == "E") {
        Course8Val = 8 * creditUnit8;
    } else if (cG8 == "F") {
        Course8Val = 0 * creditUnit8;
    }
    if (cG9 == "A") {
        Course9Val = 5 * creditUnit9;
    } else if (cG9 == "B") {
        Course9Val = 4 * creditUnit9;
    } else if (cG9 == "C") {
        Course9Val = 3 * creditUnit9;
    } else if (cG9 == "D") {
        Course9Val = 2 * creditUnit9;
    } else if (cG9 == "E") {
        Course9Val = 9 * creditUnit9;
    } else if (cG9 == "F") {
        Course9Val = 0 * creditUnit9;
    }
    totalCourseVal = (Course1Val + Course2Val + Course3Val + Course4Val + Course5Val + Course6Val + Course7Val + Course8Val + Course9Val);
    totalCreditUnit = (creditUnit1 + creditUnit2 + creditUnit3 + creditUnit4 + creditUnit5 + creditUnit6 + creditUnit7 + creditUnit8 + creditUnit9);
    gpa = totalCourseVal / totalCreditUnit;
    JOptionPane.showMessageDialog(null, "Your Grade Point Average is" + gpa, "GRADE POINT AVERAGE CALCULATOR", JOptionPane.PLAIN_MESSAGE);
    System.exit(0);

}

2 个答案:

答案 0 :(得分:0)

不要将某些变量保留为未初始化,而是将它们初始化为:

int Course1Val = 0, Course2Val = 0, Course3Val = 0, Course4Val = 0, Course5Val = 0, Course6Val = 0, Course7Val = 0, Course8Val = 0, Course9Val = 0;
String cG1 = "", cG2 = "", cG3 = "", cG4 = "", cG5 = "", cG6 = "", cG7 = "", cG8 = "", cG9 = "";

答案 1 :(得分:0)

首先,熟悉在问题/答案/评论中发布源代码。

例如:How do I format my code blocks?

回到你的问题,你试图用字符串值(“A”)比较和局部变量(尚未初始化的cG1)。确保在使用之前初始化所有变量(分配一些值)。