我有一个由15个学生对象组成的数组,其中包含一个int(学生ID)和一个数组(5个实验作业的分数)。我需要计算5个实验室中每个实验室的低,高和平均分数。我能想到的唯一方法是按列查看所有数组,但我的数组是每个学生的一行。我将如何在这些阵列上进行这些计算。 包括Student对象数组来自的Util类和用于定义对象的Student类。我只需要提供的Statistics类的帮助。 任何帮助将不胜感激!
Util.class:
import java.io.*;
import java.util.*;
import java.lang.*;
public class Util {
public static Student[] readFile(String fileName) {
Student studentArray[]=new Student[15];
try{
FileReader file = new FileReader("studentData.txt");
BufferedReader buff = new BufferedReader(file);
String line;
line = buff.readLine();
int index=0;
while(line != null){
System.out.println(line);
if(index>14){
break;
}
line = buff.readLine();
String[] result = line.split("\\s");
int sid = Integer.parseInt(result[0]);
int scores[] = new int[5];
for(int x=1;x<result.length;x++){
scores[x-1] = Integer.parseInt(result[x]);
}
Student myCSC20Student = new Student(sid, scores);
studentArray[index++] = myCSC20Student;
}
}
catch (IOException e){
System.out.println("Error: " + e.toString());
}
return studentArray;
}
}
Student.class:
import java.io.*;
import java.util.*;
public class Student {
final int LABS = 5;
private int SID;
private int scores[] = new int[LABS];
public Student(int sid, int[] scores)
{
this.SID=sid;
this.scores = scores;
}
//getters and setters for SID and scores
public int getID() {
return SID;
}
public void setID(int x) {
this.SID = x;
}
public int[] getScore() {
return scores;
}
}
Statistics.class:
import java.io.*;
import java.util.*;
public class Statistics {
final int LABS = 5;
public int[] lowscores = new int[LABS];
private int[] highscores = new int[LABS];
private float[] avgscores = new float[LABS];
public static void main(String args[]) {
Student[] studArr = Util.readFile("studentData.txt");
System.out.println("");
for(int i=0; i<=studArr.length-1; i++){
System.out.println(Arrays.toString(studArr[i].getScore()));
}
}
void calculateLow(Student[] a){
}
void calculateHigh(Student[] a){
}
void calculateAvg(Student[] a){
}
}
输出类:
import java.util.*;
public class Output{
public static void main(String[] args){
Student[] studArr = Util.readFile("studentData.txt");
Statistics statistics = new Statistics();
statistics.calculateLow(studArr);
statistics.calculateHigh(studArr);
statistics.calculateAvg(studArr);
System.out.println("Low scores:");
System.out.println(Arrays.toString(statistics.getLowscores()));
System.out.println("High scores:");
System.out.println(Arrays.toString(statistics.getHighscores()));
System.out.println("Average scores:");
System.out.println(Arrays.toString(statistics.getAvgscores()));
}
}
答案 0 :(得分:0)
import java.util.*;
public class Statistics {
final int LABS = 5;
public int[] lowscores = new int[LABS];
private int[] highscores = new int[LABS];
private float[] avgscores = new float[LABS];
public static void main(String args[]) {
Student[] studArr = Util.readFile("studentData.txt");
System.out.println();
for(int i=0; i<=studArr.length-1; i++){
System.out.println(Arrays.toString(studArr[i].getScore()));
}
Statistics statistics = new Statistics();
statistics.calculateLow(studArr);
statistics.calculateHigh(studArr);
statistics.calculateAvg(studArr);
System.out.println("Low scores:");
System.out.println(Arrays.toString(statistics.getLowscores()));
System.out.println("High scores:");
System.out.println(Arrays.toString(statistics.getHighscores()));
System.out.println("Average scores:");
System.out.println(Arrays.toString(statistics.getAvgscores()));
}
public void calculateLow(Student[] a){
for (int i = 0; i < LABS; i++) {
final int lab = i;
lowscores[lab] = Arrays.stream(a)
.mapToInt(student -> student.getScore()[lab])
.min()
.orElse(0);
}
}
public void calculateHigh(Student[] a){
for (int i = 0; i < LABS; i++) {
final int lab = i;
highscores[lab] = Arrays.stream(a)
.mapToInt(student -> student.getScore()[lab])
.max()
.orElse(0);
}
}
public void calculateAvg(Student[] a){
for (int i = 0; i < LABS; i++) {
final int lab = i;
avgscores[lab] = (float) Arrays.stream(a)
.mapToInt(student -> student.getScore()[lab])
.average()
.orElse(0);
}
}
public int[] getLowscores() {
return lowscores;
}
public int[] getHighscores() {
return highscores;
}
public float[] getAvgscores() {
return avgscores;
}
}
答案 1 :(得分:0)
@ Bogdan-Lukiyanchuk提供了一个利用Java流的解决方案。我的印象是,赋值仅用于循环和数组。因此,指向解决方案的指针类似于:
void calculateLow(Student[] a) {
// if we don't have any students, then just leave
if (a == null || a.length == 0) {
return;
}
// set to the maximum score one could possibly have
int lowScore = Integer.MAX_INT;
Student lowPersonOnTotem = null;
// process all of the students
for (Student student : a) {
int[] scores = student.getScore();
// loop over all of the scores; if the score is lower than any
// previous, update the lowScore and who had it
for (int score : scores) {
if (score < lowScore) {
lowScore = score;
lowPersonOnTotem = student;
}
}
}
System.out.printf("Lowest score of all students is %d achieved by %d\n",
lowScore, lowPersonOnTotem.getId());
}
现在,根据@markspace对评论的批评,我认为让calculateLow()
返回一个值然后将显示放在其他地方是最合适的,但该方法被标记为void
在OP的例子中。
其他方法基本上是相同的循环,但数学会改变。