gpt4 book ai didi

java - 基本的Java控制结构问题。

转载 作者:行者123 更新时间:2023-12-02 06:38:26 24 4
gpt4 key购买 nike

我是一名大学新生,我的编程作业遇到了困难。我从讲师那里得到的作业是让我用Java编写一个程序来获取学生的信息,并允许学生选择学生选修的科目,并输入分数和学分,然后用一个公式来计算年级和学科成绩点。在程序结束时,程序将能够输出学生信息(姓名、ID等)以及所有输入科目的总科目绩点、所有科目的总学分以及累积平均绩点(CGPA)。

但是,我这里遇到三个问题

  1. 我设置的循环存在问题,该循环是为了读取用户想要输入的主题数。
  2. 当我尝试打印“Grade =”+ subjectGrade);我的编译器说它尚未初始化。 GradePoint 和 subjectCreditHour 也是如此。
  3. 而且我不知道如何让程序计算总学科绩点、总学分和 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 : jcook@gmail.com
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 : jcook@gmail.com

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

学科绩点的计算公式为科目成绩点 = 成绩点 * 学时

CGPA(累积平均绩点)的计算公式为CGPA = 总科目GP/总学分

欢迎指正、批评、建议,以便今后改进。提前致谢!

最佳答案

1) I have an issue with the loop that I have set in order to read how many subjects the user wants to key in.

@Nishan 已在评论中回复。只需替换 for (int i=1;i >= subjects; i++)通过for (int i=1;i <= subjects; i++) .

<小时/>

2) When I tried to print "Grade = " + subjectGrade); my compiler says it hasn't been initialized. Same goes to the GradePoint and subjectCreditHour.

已经回答了。

<小时/>

3) And I couldn't figure out how to get the program to calculate the Total Subject Grade Points, Total Credit Hours, and CGPA. Because depending on how many numbers of subjects the user wants, I can't figure out how to get the program to take in the user's input and sum them up together

由于您拥有所需的累加器,您已经走在正确的道路上:

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

在循环中和嵌套后 if-else block ,您需要更新subjectGradePoint , totalSubjectGPtotalCreditHour每次迭代中的变量:

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

最后,在循环之后计算 CGPA :

CGPA = totalSubjectGP / totalCreditHour;

关于java - 基本的Java控制结构问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19402692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com