Java 添加由空格分隔的字符串中的数字

Java 添加由空格分隔的字符串中的数字,java,Java,我正在为班级编写一个程序,它的作用是询问用户一个班级有多少学生以及他们参加了多少次考试。然后,使用for循环,它根据您之前输入的内容询问每个学生的姓名和考试分数,一旦您输入,它会给出他们的平均考试分数和其他一些东西。我的问题是,我不知道你如何计算考试成绩的平均值。我无法让我的程序读取用户输入的测试分数,然后取平均值。提前谢谢你的帮助 import java.util.Scanner; public class GradeCalculator { public static void m

我正在为班级编写一个程序,它的作用是询问用户一个班级有多少学生以及他们参加了多少次考试。然后,使用for循环,它根据您之前输入的内容询问每个学生的姓名和考试分数,一旦您输入,它会给出他们的平均考试分数和其他一些东西。我的问题是,我不知道你如何计算考试成绩的平均值。我无法让我的程序读取用户输入的测试分数,然后取平均值。提前谢谢你的帮助

import java.util.Scanner;

public class GradeCalculator {
    public static void main(String[] args){
        //Define Variables
        int Students;
        int Exams;
        int Sum = 0;
        int ExamAverage = 0;
        String ExamScores;
        String StudentName;



        //Create Scanner
        Scanner s = new Scanner(System.in);

        //Print the First Block
        System.out.println("Welcome to GradeCalculator!");
        System.out.println("");
        System.out.println("Please enter the number of students: ");
        Students = s.nextInt();
        System.out.println("Please enter the number of exams: ");
        Exams = s.nextInt();
        s.nextLine();
        System.out.println("- - - - - - - - - - - - - - - - - - - -");

        //For Loop
        for (int i = 1; i<=Students; i++){
            System.out.println("Enter Student " + i + "'s name: ");
            StudentName = s.nextLine();
            System.out.println("Enter exam scores: " );
            ExamScores = s.nextLine();
            Sum+=Integer.parseInt(ExamScores);
            ExamAverage = Sum/Exams;
            System.out.println("Grade Statistics for " + StudentName);
            System.out.println("\t Average: " + ExamAverage);
            System.out.println("\t Letter Grade: ");
            System.out.println("\t"+ StudentName + " gets a ");
            System.out.println("- - - - - - - - - - - - - - - - - - - -");
        }

        //Print the Last Block
        System.out.println("Class Statistics:");
        System.out.println("\t Average: ");
        System.out.println("\t Lowest: ");
        System.out.println("\t Highest: ");
        System.out.println("");
        System.out.println("Done, Good Bye!");


    }

}

添加由空格分隔的字符串中的数字:

String allNumbers = "12 34 67 34 56 78";
int total = 0;
for(String str : allNumbers.split("\\s")){
   total+=Integer.parseInt(str);
}

第一步:分解问题。将问题分解为各个步骤,然后尝试一次解决一个步骤。将ExamAverage移出循环。另外,你不是在增加考试。你是如何让所有的学生都这么做的?例如,如果您输入有3名学生,那么程序将询问您有关3名学生的信息,但平均值仅适用于第一名学生,而不适用于其他两名学生。