简体   繁体   English

Java如何使用循环计算平均分数

[英]Java how to compute average scores using loops

I need to write a program in Java that computes the average score for 4 students. 我需要用Java编写一个程序,计算4个学生的平均分数。 The student will put in their 4 scores, and once they are done they will input -1 to compute the average. 学生将输入4个分数,完成后将输入-1以计算平均值。 Once this is done the program needs to move onto student 2 and so on. 完成后,程序需要移至学生2上,依此类推。 At the end it is supposed to display the highest average from the 4 student average test scores. 最后,应该显示4个学生平均考试成绩中最高的平均分数。 Here is what it should look like when it is run: 这是运行时的外观:

Student 1 学生1

Enter your score: 100 输入您的分数:100

Enter your score: 90 输入您的分数:90

Enter your score: 80 输入您的分数:80

Enter your score: 70 输入您的分数:70

Enter your score: -1 * once the student enters -1 it should compute average 输入您的分数:-1 *一旦学生输入-1,就应该计算平均值

Average Score = 85. 平均分数= 85。

Student 2 学生2

Enter your score: 90 输入您的分数:90

ETC 等等

ETC 等等

The problem with my code is that the average is only correct for the first student. 我的代码的问题在于,平均值仅对第一个学生正确。 When I input -1 to get the average for the second student, the calculation is incorrect. 当我输入-1以获取第二个学生的平均值时,计算不正确。 We are only allowed to use loops. 我们只允许使用循环。 The only hints I was given were that we are supposed to write an outer loop that iterates 4 times, write an inner loop that loops as long as the student has scores to enter, inside the inner loop prompt the user to enter their last score or -1 to compute average. 我得到的唯一提示是,我们应该编写一个迭代4次的外部循环,编写一个只要学生有分数输入就循环的内部循环,在内部循环中提示用户输入他们的最后分数或-1以计算平均值。 I don't want you guys to do the project for me but to just set me in the right direction. 我不想你们为我做这个项目,而只是让我朝着正确的方向前进。 I feel like I am not using the right loop. 我觉得我没有使用正确的循环。

import java.util.Scanner;

public class TestScore

{

     public static void main(String[]args)
     {

       double score = 0;

       double totalScore = 0;

       double count = 0;

       double average = 0;

       Scanner input = new Scanner(System.in);


      System.out.println("Student 1");
      System.out.printf("Enter Your Score: ");
      score = input.nextDouble();



      while (score != -1){
          System.out.printf("Enter Your Score: ");
          totalScore = totalScore + score;
          score = input.nextDouble();
          count++;
          average = totalScore / count;

          if (score == -1){
              System.out.printf("Average Score = %.2f\n ",average);
              count = 0;
              score = 0;
              totalScore = 0;
              average = 0;
              System.out.println("Student 2");
              System.out.printf("Enter Your Score: ");
              score = input.nextDouble ();
              count++;
              average = totalScore / count;
          }   

      }  



}



}

You haven't explicitly asked a question so I'll try and comply to the "set me in the right direction" part. 您尚未明确提出问题,因此我将尝试并遵守“为我设定正确的方向”部分。

I'd suggest re-formatting the loop structure to a cleaner one, like this: 我建议将循环结构重新格式化为更简洁的结构,如下所示:

double total;
for(int student = 1; student <= 4; student++) {
    System.out.printf("Student %d\n", student);
    double sum = 0, count = 0;

    while(true) {
        System.out.printf("Enter your score: ");
        double input = scanner.nextDouble();
        if(input == -1) break;
        sum += input;
        count++;
    }
    total += sum;

    System.out.printf("Average: %.2f\n", sum / count);
}

System.out.printf("Total: %.2f\n", total);

Hope that's enough to give you some pointers. 希望足够给您一些指导。

edit: forgot to take care of total 编辑:忘记照顾total

So, you wish to iteratively go through all the input and just remember the maximum one. 因此,您希望迭代遍历所有输入并仅记住最大输入。 Make an integer variable max and after each student, just change it if needed. 设置一个整数变量max,每位学生之后,如有需要,只需对其进行更改。 (It's zero by default jn Java) As for the calculation for each student, you shouldn't be checking for the failed " score != - 1" condition in each iteration. (在Java中,默认值为零)对于每个学生的计算,您不应该在每次迭代中检查失败的“ score!=-1”条件。 Instead, you should do the final calculations after the while loop. 相反,您应该在while循环之后进行最终计算。 (average, possible update of the maximum, resetting the variables, etc. ) You also need the outer loop (in the stated code, these calculations are done for one student only) which you would control in a different manner. (平均,可能的最大值更新,重置变量等)。您还需要用不同的方式控制的外部循环(在所述代码中,这些计算仅针对一名学生进行)。

Also, if you need to use only 4 grades, you might want to consider using the for loop. 另外,如果只需要使用4个等级,则可能要考虑使用for循环。

You can try with this :D 你可以试试这个:D

public static void main (String[] args) throws java.lang.Exception
{
    double average = 0;
    double i = 0;
    int student = 0;
    boolean flag = true;
    Scanner input = new Scanner(System.in);
    while(flag)
    {
        System.out.printf("Student: ");
        System.out.println(student);
        System.out.print("Enter Your Score: ");

        double score = input.nextDouble();
        if(score!=-1){
            average=average+score;
            i=i+1;
        }

        if(score==-1){
            System.out.printf("Average: ");
            System.out.println(average/i);
            //reset values
             average = 0;
             i = 0;
            student=student+1;
        }
        if(score==-2){
            //you need break the while in some moment.
            flag = false;
        }
    }
}

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

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