我有这个任务,我需要使用Java来创建一个for循环来计算每个学生的平均值'得分了。教师必须输入每个学生的考试成绩数,然后循环将针对每个学生进行迭代。
import java.util.Scanner;
public class average {
public static void main(String[] args) {
int count = 0;
int student;
int sum = 0;
int grade;
int average = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of students:");
student = keyboard.nextInt();
System.out.println("Enter number of test scores per student: ");
count = keyboard.nextInt();
for (int s = 0; s < student; s++) {
System.out.println("Student number " + (s + 1) );
for (int i = 0; i < count; i++) {
System.out.println("Enter score no." + (i + 1) + " : ");
grade = keyboard.nextInt();
sum += grade;
average = sum/count;
}
System.out.println("The average is " + average);
}
}
}
答案 0 :(得分:0)
打印平均值后得到sum = 0
刚刚在下面的循环中显示
for (int s = 0; s < student; s++) {
System.out.println("Student number " + (s + 1));
for (int i = 0; i < count; i++) {
System.out.println("Enter score no." + (i + 1) + " : ");
grade = keyboard.nextInt();
sum += grade;
average = sum / count;
}
System.out.println("The average is " + average);
sum = 0; //THIS IS REQUIRED
}