Java 成绩计算器程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26318047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grade calculator program
提问by sam
I'm totally stuck on this project, in which a user is supposed to enter number of students in a class, and number of exams taken. Then they enter each student's name, and then the exam scores for that student on a single line separated by blank spaces. The program calculates each student's average and the corresponding letter grade.
我完全被这个项目困住了,在这个项目中,用户应该输入一个班级的学生人数,以及参加的考试数量。然后他们在一行中输入每个学生的姓名,然后将该学生的考试成绩以空格分隔。该程序计算每个学生的平均成绩和相应的字母成绩。
This is what I have so far:
这是我到目前为止:
public class proj2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Welcome to Gradecalculator!");
System.out.println("Please enter the number of students:");
int students = s.nextInt();
System.out.println("Please enter the number of exams:");
int exams = s.nextInt();
int i = 0;
int studentnumber = 1;
int sum = 0;
while (i < students) {
double average = sum/exams;
System.out.println("Enter student " + studentnumber++ + "'s name :");
String studentname = s.next();
System.out.println("Enter exam scores :");
for (; i < exams; i++) {
int n = s.nextInt();
sum+=n;
if (n < 0) {
System.out.println("Invalid exam scores, reenter: ");
}
}
if (average <= 100 && average >= 90) {
System.out.println("Letter grade: A");
System.out.println(studentname + " gets 4 stars! ****");
} if (average <= 89 && average >= 80) {
System.out.println("Letter grade: B");
System.out.println(studentname + " gets 3 stars! ***");
} if (average <= 79 && average >= 70) {
System.out.println("Letter grade: C");
System.out.println(studentname + " gets 2 stars! **");
} if (average <= 69 && average >= 60) {
System.out.println("Letter grade: D");
System.out.println(studentname + " gets 1 star! *");
} if (average <= 59) {
System.out.println("Letter grade: F");
System.out.println(studentname + " gets 0 stars!");
}
}
}
}
This is the output I get as of now:
这是我现在得到的输出:
Welcome to Gradecalculator!
Please enter the number of students: 3 Please enter the number of exams: 3 Enter student 1's name : sam Enter exam scores : 80 80 80 Letter grade: F sam gets 0 stars!
请输入学生人数:3 请输入考试人数:3 输入学生 1 的姓名:sam 输入考试成绩:80 80 80 字母等级:F sam 获得 0 星!
Obviously three 80's should be a B first of all, so I'm obviously not calculating the average properly, but I can't figure out why.
显然三个 80 年代首先应该是 B,所以我显然没有正确计算平均值,但我不知道为什么。
采纳答案by Eran
Your conditions should look like this :
您的条件应如下所示:
if (average <= 100 && average >=90)
not like this :
不是这样的:
if (average <= 100 & average >=90)
You want to use logical AND and not bitwise AND.
您想使用逻辑与而不是按位与。
Another problem is that you reset sum
to 0 in each iteration, so you are not actually accumulating the sum.
另一个问题是您sum
在每次迭代中都重置为 0,因此您实际上并未累积总和。
Finally, the calculation of the average and all the conditions should be outside the for loop, since you want to read all the inputs before calculating the average.
最后,平均值和所有条件的计算应该在 for 循环之外,因为您想在计算平均值之前读取所有输入。
回答by Qadir Hussain
You have done a small mistake here.
你在这里犯了一个小错误。
if (average <= 100 & average >= 90)
I think you're trying to use logical AND
use double &
like this.
我认为您正在尝试像这样使用逻辑AND
使用 double &
。
if (average <= 100 && average >= 90)
and you need to change your code little more. You need to declare sum
outside for
loop because every time its getting reset to 0
.
并且您需要稍微更改一下代码。您需要声明sum
外部for
循环,因为每次它重置为0
.
int sum = 0;
for (; i < exams; i++) {
int n = s.nextInt();
sum+=n;
}
double average = sum/exams;