gpt4 book ai didi

java - 使用数组按姓名和分数对学生进行排序

转载 作者:行者123 更新时间:2023-11-30 06:27:25 25 4
gpt4 key购买 nike

<分区>

我昨晚发布了一个关于这个的问题,但仍在挣扎。我需要首先按姓名(以这种格式的姓氏、名字)对文本文件中的学生列表进行排序,然后按考试成绩对学生列表进行排序。我不知道文本文件中会有多少学生,除了少于 100 个之外。我必须使用 compareTo 方法(我认为我做对了)和一个数组(我不认为我'我正确使用它)。我连续几个小时都在搞这个,但我似乎就是不明白。教科书似乎真的对我没有任何帮助。如何让我的应用类打印排序的学生姓名/成绩?

其他规范是获取分数的平均值,并在低于平均值的任何分数旁边进行评论。但是,我可以在整理好排序后继续处理这些内容。

这是我的代码...

 package CH11AS8App;

import java.io.FileReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


public class kjkkkkkkkkkkk {

public static void main( String[] args ) throws Exception {
// TODO Auto-generated method stub

Scanner aScanner = new Scanner(
new FileReader("src//chapt11//ch11AS8data.txt"));



int numOfStudents = 100;
Student[] studentArray = new Student[numOfStudents];
Scanner sc = null;

int counter = 0;
while (aScanner.hasNext()) {
sc = new Scanner(aScanner.nextLine());
String lastName = sc.next();
String firstName = sc.next();
int score = sc.nextInt();


studentArray[counter++] = new Student(lastName, firstName, score);

studentArray[counter] = new Student(lastName, firstName, score);

int average= 0;
int sum = 0;
sum += score;
if (counter < numOfStudents);
average = sum / counter;
if (score <= (average - 10)) {
System.out.println("Score 10 points under average");
System.out.println("Student Average:" + average);
}



sc.close();


// Display Welcome Message
System.out.println("Welcome to the Student Scores Application.\n");

//Sort Names
Arrays.sort(studentArray,0,counter, new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
return s1.getLastName().compareTo(s2.getLastName());
}
});

System.out.println();
System.out.println("Student list by name:");
System.out.println();
for(int j = 0; j < counter; j++){
System.out.println(studentArray[j]);

}

//Sort Scores
Arrays.sort(studentArray,0,counter, new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
return Integer.valueOf(s2.getScore()).
compareTo(Integer.valueOf(s1.getScore()));
}
});


System.out.println();
System.out.println();
System.out.println("Student list by score:");
System.out.println();
for(int j = 0; j < counter; j++){
System.out.println(studentArray[j]);

}


//Close Scanner
aScanner.close();

static class Student implements Comparable<Student> {

//Instance Variables
private String firstName;
private String lastName;
private int score;

//Getters & Setters
public Student( String firstName, String lastName, int score ) {
this.firstName = firstName;
this.score = score;
this.lastName = lastName;
}
public int getScore() {
return score;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}


//CompareTo Method
@Override
public int compareTo( Student s ) {

if( !firstName.equalsIgnoreCase( s.firstName ) ) {

return firstName.compareToIgnoreCase( s.firstName );
}

if( !lastName.equalsIgnoreCase( s.lastName ) ) {

return lastName.compareToIgnoreCase( s.lastName );
}
return (new Integer(score)).compareTo((new Integer(s.score)));
}
@Override public String toString(){ return lastName + ", "+ firstName +" : "+score; }


}

}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com