在下面的代码中,我需要读取每个学生的五个学生名称和五个测验的标记列表,它们将名称加载到String类型的ArrayList中,并在Integer类型的ArrayList中加载测验标记。我已经通过两个不同的ArrayLists解决了这个问题,我希望将它们结合起来但不确定。
以下代码读取五个学生名称,并在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
答案 0 :(得分:4)
这可以通过一个循环更简单地完成,假设您知道数据将采用正确的形式(名字和姓氏后跟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);
}
}