【问题标题】:Combining Arrays in Java在 Java 中组合数组
【发布时间】:2016-07-12 21:12:10
【问题描述】:

在下面的代码中,我需要读取一个包含五个学生姓名和每个学生五个测验的分数的列表,这会将姓名加载到 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

【问题讨论】:

    标签: java arraylist


    【解决方案1】:

    这可以通过一个循环更简单地完成,假设您知道数据将采用正确的形式(名字和姓氏后跟 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);
        }
    }
    

    【讨论】:

    • 我在哪里将它添加到我的代码中?不幸的是,我们还没有使用 private
    • int average 应该是 double average 您还缺少 main 的右大括号。
    • 只需将旧的主要方法替换为我重写的方法。您可以像在现有代码中声明公共方法一样声明私有方法 - 只需将它们添加到主方法下方即可。
    • 我应该用我制作的第一个程序替换那个 main 方法吗?
    • @janedone 这个答案看起来是独立的,可以作为Student.java 文件运行
    猜你喜欢
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 2012-09-21
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多