我一直试图解决这个问题,要么我考虑得太多,要么我只是遗漏了一些东西。我需要用户输入学生人数,得到了。然后我需要根据用户输入的数字为每个学生输入一个单独的考试成绩。这就是我被困住的地方。然后我需要根据这些测试等级计算总和,并计算平均值。任何帮助将不胜感激。到目前为止我有这个:
import java.util.*;
public class computeSumAverage {
public static void main(String[] args) {
Scanner kbd = new Scanner(System. in );
System.out.print("Please enter the number of students = ");
int student = kbd.nextInt();
System.out.print("Please enter " + student + " test grades = ");
int count;
double grades = kbd.nextDouble();
double sum = kbd.nextDouble();
double average;
for (count = 0; count <= 100; count++);
sum = (grades++);
average = (sum / student);
System.out.println("The sum of the numbers is = " + sum + "." + "\n");
System.out.println("The average of the numbers is = " + average + ".");
}
}
答案 0 :(得分:2)
试试这个。它应该工作。
public static void main (String[] args) {
Scanner kbd = new Scanner (System.in);
System.out.print ("Please enter the number of students = ");
int count = kbd.nextInt();
double sum = 0;
double average = 0;;
for (int student = 0; student < count; student ++){
System.out.print ("Please enter Student: " + String.valueOf(student +1) + " test grades = ");
double grades = kbd.nextDouble();
sum += grades;
}
average = (sum / count);
System.out.println ("The sum of the grades is = " + sum + "." + "\n");
System.out.println ("The average of the grades is = " + average + ".");
}
您的代码有什么问题。
是没用的,因为它无助于你实现你想做的事情和;立即停止。循环没有核心。你应该循环学生的数量。
在循环内,您应该阅读成绩并提示用户输入每个学生的成绩。
你做的总和还不错。
在编码你正在做的事情之前,你应该考虑一下。
答案 1 :(得分:0)
这一行:
for (count = 0; count <= 100; count ++);
应该是:
for (count = 0; count <= 100; count ++)
请参阅: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
答案 2 :(得分:0)
你有多少学生进入student
,之后你应该在一个循环(例如for
循环)中要求成绩,每个学生的数量是你拥有的学生数量的多少,但你要问的是仅在循环外的等级一次...
请阅读有关(){}和{和}的内容。
工作代码段是:
...
double sum = 0;
for ( int i = 1; i <= students; i++ ) {
System.out.println("Grade for student " + i ":");
double grad = kdb.nextDouble();
sum = sum + grade;
}
double avg = sum / students;
...
答案 3 :(得分:0)
您的解决方案中缺少一些内容:
例如:
public static void main(String[] args) {
Scanner kbd = new Scanner(System. in );
System.out.print("Please enter the number of students = ");
int students = kbd.nextInt();
System.out.print("Please enter " + students + " test grades = ");
double sum = 0;
for (int count = 0; count < students; count++) {
double grade = kbd.nextDouble();
sum += grade;
}
double average = (sum / students);
System.out.println("The sum of the numbers is = " + sum + "." + "\n");
System.out.println("The average of the numbers is = " + average + ".");
}