简体   繁体   English

成绩计算器程序

[英]Grade calculator program

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,所以我显然不能正确地计算平均值,但是我不知道为什么。

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循环之外,因为您要在计算平均值之前读取所有输入。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM