gpt4 book ai didi

java - 用 Java 调试我的字母成绩计算器

转载 作者:行者123 更新时间:2023-11-30 01:44:56 26 4
gpt4 key购买 nike

我应该创建一个程序来执行以下操作:

A student must take two quizzes and two exams during the semester. One quiz and one exam are dropped from the calculation of their final grade. The remaining quiz counts as 40% of the final grade and the exam counts for 60% of their final grade.

The final grade is calculated on a straight scale: 90% and above is an “A”, less than 90% to 80% is a B, etc.This program should allow a professor to enter a student’s name, id number, two quiz scores and two exam scores and then compute and output the student’s final grade.

下面是我写的代码。它执行得很好,没有任何错误,我感觉我已经正确地完成了所有事情;但是,无论我输入什么分数,程序都会返回字母等级“A”。

public static void main(String[] args)
{
//Declare all variables
String name;
String idNum;
int q1;
int q2;
int e1;
int e2;
int bestQuiz;
int bestExam;
double score;
char letterGrade;

Scanner kbd;
kbd = new Scanner(System.in);

System.out.println("Enter student's name: ");
name = kbd.nextLine();
System.out.println("Enter student's ID number: ");
idNum = kbd.nextLine();
System.out.println("Enter the quiz scores: ");
q1 = kbd.nextInt();
q2 = kbd.nextInt();
System.out.println("Enter the exam scores: ");
e1 = kbd.nextInt();
e2 = kbd.nextInt();

bestQuiz = max(q1, q2);
bestExam = max(e1, e2);

score = computeRawPercentage(bestQuiz, bestExam);

letterGrade = finalGrade( score );

System.out.print(name + " " + idNum + " ");
System.out.println("Final Grade: " + letterGrade);

}

//Max method
public static int max(int n1, int n2){
int big;
if (n1 > n2){
big = n1;
}
else {
big = n2;
}
return big;
}

//computeRawPercentage method
public static double computeRawPercentage(int quizScore, int examScore){
return ((quizScore * .4)+ (examScore * .6))*100;
}

//finalGrade method
public static char finalGrade(double grade){

char letterGrade;

if (grade >= 90.0)
letterGrade = 'A';
else if ((grade >= 80.0)&&(grade < 90.0))
letterGrade = 'B';
else if ((grade >= 70.0)&&(grade < 80.0))
letterGrade = 'C';
else if ((grade >= 60.0)&&(grade < 70.0))
letterGrade = 'D';
else
letterGrade = 'F';

return letterGrade;

}




}

最佳答案

你的问题是这一行:

return ((quizScore * .4)+ (examScore * .6))*100;

在这里,您将您的分数乘以 100,这将给您一个类似于 7100 的分数,而实际上它应该是 71(其中明显高于 90.0)。删除100乘法即可:

return ((quizScore * .4) + (examScore * .6));

测试运行:

Enter student's name: 
TestName
Enter student's ID number:
12345
Enter the quiz scores:
55
65
Enter the exam scores:
65
86
77.6
TestName 12345 Final Grade: C

假设您输入 75% 的分数为 75

关于java - 用 Java 调试我的字母成绩计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58457589/

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