java Java如何使用循环计算平均分数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28638150/
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
Java how to compute average scores using loops
提问by John Doe
I need to write a program in Java that computes the average score for 4 students. The student will put in their 4 scores, and once they are done they will input -1 to compute the average. Once this is done the program needs to move onto student 2 and so on. At the end it is supposed to display the highest average from the 4 student average test scores. Here is what it should look like when it is run:
我需要用 Java 编写一个程序来计算 4 名学生的平均分数。学生将输入他们的 4 个分数,完成后他们将输入 -1 来计算平均值。完成此操作后,程序需要转移到学生 2 上,依此类推。最后,它应该显示 4 名学生平均考试成绩的最高平均值。下面是它运行时的样子:
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. 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. 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.
我的代码的问题是平均值只对第一个学生正确。当我输入 -1 以获得第二个学生的平均值时,计算不正确。我们只允许使用循环。我得到的唯一提示是,我们应该编写一个迭代 4 次的外循环,编写一个只要学生有分数输入就循环的内循环,在内循环内提示用户输入他们的最后一个分数或-1 计算平均值。我不希望你们为我做这个项目,而只是让我朝着正确的方向前进。我觉得我没有使用正确的循环。
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;
}
}
}
}
回答by Michael Borkowski
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
回答by reaponja
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. (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. Instead, you should do the final calculations after the while loop. (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.
因此,您希望迭代地遍历所有输入并只记住最大的一个。创建一个整数变量 max 并在每个学生之后,根据需要更改它。(默认情况下 jn Java 为零)至于每个学生的计算,您不应该在每次迭代中检查失败的“分数!= - 1”条件。相反,您应该在 while 循环之后进行最终计算。(平均值、最大值的可能更新、重置变量等。)您还需要外部循环(在所述代码中,这些计算仅针对一名学生完成),您将以不同的方式进行控制。
Also, if you need to use only 4 grades, you might want to consider using the for loop.
此外,如果您只需要使用 4 个等级,您可能需要考虑使用 for 循环。
回答by Kevin F
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;
}
}
}