在下面的代码中,我需要读入五个学生姓名的列表,并为每个学生进行五个测验的标记,这会将名称加载到String类型的ArrayList中,并将测验标记加载到Integer类型的ArrayList中。我已经通过两个不同的ArrayList分解了这个问题,我想将它们组合在一起但不确定。

以下代码读取五个学生的姓名,并将姓名加载到String类型的ArrayList中

import java.util.ArrayList;
public class QuizAveragee
{
    public static void main( String[] args ) {
        final int NAMELIMIT = 5 ;
        final int QUIZLIMIT = 5 ;
        ArrayList<String> sNames = new ArrayList<String>();
        ArrayList<String> sFamily = new ArrayList<String>();
        Scanner in = new Scanner(System.in);
        // Load the five names of the students in the arraylist
        for(int i = 1; i<=NAMELIMIT; i++)
        {
            String[] input = in.nextLine().split("\\s+");

            sNames.add(input[0]);
            sFamily.add(input[1]);
        }
        for(int i=0; i<NAMELIMIT; i++)
        {
            System.out.println("Name: " + sNames.get(i) + " " + sFamily.get(i));
        }
        System.out.println();
    }
}


输入以下内容:

Sally Mae 90 80 45 60 75
Charlotte Tea 60 75 80 90 70
Oliver Cats 55 65 76 90 80
Milo Peet 90 95 85 75 80
Gavin Brown 45 65 75 55 80


它产生:

Name: Sally Mae
Name: Charlotte Tea
Name: Oliver Cats
Name: Milo Peet
Name: Gavin Brown


然后,我需要制作程序的一部分,该程序将为每个学生阅读五次测验,并将测验标记加载到整数类型的ArrayList中。这就是我为这部分生成的。

import java.util.ArrayList;
import java.util.Scanner;
public class heya
{
    public static final int QUIZLIMIT = 5;
    public static Scanner readQuiz;

    public static void main(String[] args)
    {
        readQuiz = new Scanner(System.in);

        while (readQuiz.hasNextLine()) {
            ArrayList<Integer> quizMarks = readArrayList(readQuiz.nextLine());
            computerAverage(quizMarks);
        }
    }

    // Load quiz marks
    public static ArrayList<Integer> readArrayList(String input)
    {
        ArrayList<Integer> quiz = new ArrayList<Integer>();
        Scanner readQuiz = new Scanner(input);
        int i = 1;
        while (i <= QUIZLIMIT)
        {
            if (readQuiz.hasNextInt()) {
                quiz.add(readQuiz.nextInt());
                i++;
            }
            else {
                readQuiz.next(); // Toss the next read token
            }
        }
        return quiz;
    }

    // Compute the average of quiz marks
    public static void computerAverage(ArrayList<Integer>quiz)
    {
        int total = 0 ;
        for(Integer value : quiz)
        {
            total = total + value;
        }
        System.out.println("Quiz Avg: "+ (total/QUIZLIMIT));
    }
}


它给出了输出:

Quiz Avg: 70
Quiz Avg: 75
Quiz Avg: 73
Quiz Avg: 85
Quiz Avg: 64


但是,我需要结合这些程序,但我不确定该怎么做。给定的输入:

Sally Mae 90 80 45 60 75
Charlotte Tea 60 75 80 90 70
Oliver Cats 55 65 76 90 80
Milo Peet 90 95 85 75 80
Gavin Brown 45 65 75 55 80


应该给:

Name: Sally Mae Quiz Avg: 70
Name: Charlotte Tea Quiz Avg: 75
Name: Oliver Cats Quiz Avg: 73
Name: Milo Peet Quiz Avg: 85
Name: Gavin Brown Quiz Avg: 64

最佳答案

假设您知道数据格式正确(名字和姓氏后跟5个等级),并且不需要存储名称或等级供以后使用,则可以通过一个循环更轻松地完成此操作。

public class QuizAveragee {
  private static final int NAMELIMIT = 5;

  public static void main(String[] args) {
    //these lines are not needed but OP asked for the values to be stored in arrays
    ArrayList<String> names = new ArrayList<>();
    ArrayList<Double> averages = new ArrayList<>();
    Scanner in = new Scanner(System.in);

    for (int i = 0; i < NAMELIMIT; i++) {
        String line = in.nextLine();
        String[] words = line.split(" ");
        String name = words[0] + " " + words[1];
        double average = findAverage(words[2], words[3], words[4], words[5], words[6]);
        System.out.println("Name : " + name + " Quiz Avg: " + average);

        //these lines are not needed but OP asked for the values to be stored in arrays
        names.add(name);
        averages.add(average);
    }
  }

  private static double findAverage(String a, String b, String c, String d, String e) {
    double sum = Double.parseDouble(a) + Double.parseDouble(b) + Double.parseDouble(c) + Double.parseDouble(d) + Double.parseDouble(e);
    return (sum / NAMELIMIT);
  }
}


如果确实需要存储这些值供以后使用,我建议利用Java是一种面向对象的语言的事实,并声明一个Student对象,该对象可以容纳学生的姓名和成绩。您可以这样做:

public class Student {
  private ArrayList<Integer> grades;
  private String fName;
  private String lName;

  public Student(String inputLine) {
    grades = new ArrayList<>();
    String[] lineSplit = inputLine.split(" ");
    fName = lineSplit[0];
    lName = lineSplit[1];
    for (int i = 2; i < lineSplit.length; i++) {
      grades.add(Integer.parseInt(lineSplit[i]));
    }
  }

  private double computeAvg() {
    double sum = 0;
    for (Integer grade : grades) {
      sum = sum + grade;
    }
    return sum / grades.count();
  }

  @Override
  public String toString() {
    return "Name: " + fName + " " + lName + " Quiz Avg: " + computeAvg();
  }
}

private static final int NAMELIMIT = 5;

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    for (int i = 0; i < NAMELIMIT; i++) {
        String line = in.nextLine();
        Student s = new Student(line);
        System.out.println(s);
    }
}

10-08 02:08