我是大学新生,我在编程作业上遇到了麻烦。我从讲师那里得到的作业是为我编写一个Java语言程序来获取学生的信息,并允许学生选择该学生要学习的科目,并输入分数和学分,然后是一个公式来计算年级和学科成绩点。在该程序的最后,该程序将能够输出学生信息(姓名,ID等)和所有已输入学科的总学科成绩点,所有学科的总学时数以及累计平均绩点(CGPA)。

但是,我这里有三个问题


为了读取用户要键入的主题数,我设置的循环出现了问题。
当我尝试打印“ Grade =” + subjectGrade)时;我的编译器说它还没有初始化。同样,GradePoint和subjectCreditHour也是如此。
而且我不知道如何获得该程序来计算总科目成绩,总学时和CGPA。因为取决于用户想要多少个主题,我无法弄清楚如何使程序接受用户的输入并将它们汇总在一起


我的代码是这样的:

package javaquiz1;

import java.util.Scanner;

/**
 *
 * @author jerem_000
 */
public class JavaQuiz1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner (System.in);

        String name;
        int ID;
        int tel;
        String email;
        int subjects;

        String subjectName;
        int subjectCreditHour;
        int subjectMark;
        String subjectGrade;
        double GradePoint;

        double subjectGradePoint;
        double CGPA;
        double totalSubjectGP;
        int totalCreditHour;

        System.out.print("Please input student's name : ");
        name = input.nextLine();
        System.out.print("Please input student's ID : ");
        ID = input.nextInt();
        System.out.print("Please input student's telephone number : ");
        tel = input.nextInt();
        System.out.print("Please input student's email : ");
        email = input.next();
        System.out.print("Please input number of subjects : ");
        subjects = input.nextInt();

        for (int i = 1; i >= subjects ; i++) {  //I'm having an issue with this loop
        System.out.println("Subject " + i +  " : Please input the following"); //I placed the variable i there in order to make the program print something like "Subject 1 , Subject 2, Subject 3 etc". Depending on the user's number of subjects input
        System.out.print("Subject name : ");
        subjectName = input.next();
        System.out.print("Credit Hour : ");
        subjectCreditHour = input.nextInt();
        System.out.print("Mark : ");
        subjectMark = input.nextInt();

        if ( subjectMark >= 80 ) {
            subjectGrade = "A";
            GradePoint = 4.0;
        } else if (subjectMark < 80) {
            subjectGrade = "B+";
            GradePoint = 3.5;
        } else if (subjectMark < 70) {
            subjectGrade = "B";
            GradePoint = 3.0;
        } else if (subjectMark < 65) {
            subjectGrade = "C+";
            GradePoint = 2.5;
        } else if (subjectMark < 55) {
            subjectGrade = "C";
            GradePoint = 2.0;
        } else if (subjectMark < 50) {
            subjectGrade = "D";
            GradePoint = 1.0;
        } else  {
            subjectGrade = "F";
            GradePoint = 0.0;
        }
    }

        System.out.println("Grade = " + subjectGrade);
        System.out.println("Subject Grade Point = " + (GradePoint * subjectCreditHour)); //I'm having a problem with the subjectGrade, GradePoint, and subjectCreditHour, it says variable might have not been initialized

        System.out.println("Name : " + name);
        System.out.println("ID : " + ID);
        System.out.println("Tel : " + tel);
        System.out.println("email : " + email);

        System.out.print("Total subject Grade Points = "  );
        System.out.print("Total Credit Hours = " );
        System.out.print("Cumulative Grade Point Average =");  //On this 3 system.out.prints, I can't seem to think of a way to read the Grade Point, Total Credit Hours, and CGPA, and add them all together

    }
}


我也有一个示例程序输出示例:

Please input student's name : James Cook
Please input student's ID : 0106578
Please input student's tel : 010783938
Please input student's e-mail : [email protected]
Please input number of subjects : 3

Subject 1 : Please input the following
Subject name : Fundamentals of Programming
Credit Hour : 4
Mark : 78
Grade : B+
Subject Grade Point : 14.0

Subject 2 : Please input the following
Subject name : English
Credit Hour : 3
Mark : 85
Grade : A
Subject Grade Point : 12.0

Subject 3 : Please input the following
Subject name : Computer Fundamentals
Credit Hour : 3
Mark : 78
Grade : B+
Subject Grade Point : 10.5

Name : James Cook
ID : 0106578
tel :  010783938
e-mail : [email protected]

Total subject Grade Point = 36.5
Total Credit Hours = 10
CGPA = 3.65


计算学科成绩的公式是
subjectGradePoint = GradePoint * CreditHour

并且计算CGPA(累积平均成绩)的公式为
CGPA = totalSubjectGP / totalCreditHours

纠正,批评和建议将受到欢迎,以便将来进行改进。提前致谢!

最佳答案

1)我为了查看如何设置的循环而遇到了问题
  用户想要键入的许多主题。


@Nishan已在评论中回答。只需将for (int i=1;i >= subjects; i++)替换为for (int i=1;i <= subjects; i++)




  2)当我尝试打印“ Grade =” + subjectGrade)时;我的编译器说
  尚未初始化。同样,GradePoint和
  subjectCreditHour。


已经回答。




  3)而且我不知道如何获取程序来计算
  总科目成绩,总学时和CGPA。因为
  根据用户想要多少个学科,我不能
  弄清楚如何使程序接受用户的输入和求和
  他们在一起


由于您拥有所需的累加器,因此您已经处于正确的方法:

double subjectGradePoint = 0d;
double CGPA = 0d;
double totalSubjectGP = 0d;
int totalCreditHour = 0;


循环循环并嵌套if-else块之后,您需要在每次迭代中更新subjectGradePointtotalSubjectGPtotalCreditHour变量:

subjectGradePoint = GradePoint * CreditHour;
totalSubjectGP += subjectGradePoint;
totalCreditHour += CreditHour;


最后,在循环之后计算CGPA

CGPA = totalSubjectGP / totalCreditHour;

07-24 09:38