Java 使用数组按姓名和分数对学生进行排序
昨晚我发布了一个关于这个的问题,但我仍然在努力。我需要对文本文件中的学生列表进行排序,首先按姓名(这种格式为姓氏、名字),然后按考试成绩。我不知道文本文件中会有多少学生,除了不到100人。我必须使用compareTo方法(我认为我做得对)和数组(我认为我没有正确地使用它)。我已经连续几个小时胡闹了,我就是不明白。这本教科书似乎对我没有任何帮助。如何让我的应用程序类打印已排序的学生姓名/成绩 其他说明是获得平均分数,并在低于平均分数的任何分数旁边发表评论。不过,在整理好分类后,我可以继续处理这些问题 这是我的密码Java 使用数组按姓名和分数对学生进行排序,java,arrays,sorting,comparable,compareto,Java,Arrays,Sorting,Comparable,Compareto,昨晚我发布了一个关于这个的问题,但我仍然在努力。我需要对文本文件中的学生列表进行排序,首先按姓名(这种格式为姓氏、名字),然后按考试成绩。我不知道文本文件中会有多少学生,除了不到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();
包CH11AS8App;
导入java.io.FileReader;
导入java.util.array;
导入java.util.Comparator;
导入java.util.Scanner;
公共类KKKKKKKK{
公共静态void main(字符串[]args)引发异常{
//TODO自动生成的方法存根
扫描仪aScanner=新扫描仪(
新的文件阅读器(“src//chapt11//ch11AS8data.txt”);
学生人数=100;
学生[]学生阵列=新学生[NumofStudentArray];
扫描仪sc=空;
int计数器=0;
while(aScanner.hasNext()){
sc=新扫描仪(aScanner.nextLine());
字符串lastName=sc.next();
字符串firstName=sc.next();
int score=sc.nextInt();
studentArray[counter++]=新学生(姓、名、分数);
学生阵列[计数器]=新学生(姓、名、分数);
整数平均=0;
整数和=0;
总和+=分数;
如果(柜台<学生);
平均值=总和/计数器;
如果(score您的错误出现在compareTo()方法的最后一行
中:
改变
return examScore = s.examScore;
到
您希望返回一个差异,而不是设置实例变量!我认为您应该将比较中的最后一行更改为如下所示:
return (new Integer(examScore)).compareTo((new Integer(s.examScore));
public static void main(String[] args) throws Exception {
Scanner aScanner = new Scanner(
new FileReader("src//chapt11//ch11AS8data.txt"));
System.out.println("Welcome to the Student Scores Application.\n");
int nStudent = 100;
Student[] studentArray = new Student[nStudent];
Scanner lineScanner = null;
int counter = 0;
while (aScanner.hasNext()) {
lineScanner = new Scanner(aScanner.nextLine());
String lastName = lineScanner.next();
String firstName = lineScanner.next();
int examScore = lineScanner.nextInt();
System.out.println("Student " + counter + " " + firstName + " "
+ lastName + " " + +examScore);
studentArray[counter++]=new Student(lastName, firstName, examScore);
lineScanner.close();
}
for(int j = 0; j < counter; j++){
System.out.println(studentArray[j]);
}
//sort based on first name
Arrays.sort(studentArray,0,counter, new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
return s1.getFirstName().compareTo(s2.getFirstName());
}
});
System.out.println("Students sorted by first name in ascending order");
for(int j = 0; j < counter; j++){
System.out.println(studentArray[j]);
}
//sort based on score
Arrays.sort(studentArray,0,counter, new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
return Integer.valueOf(s1.getExamScore()).
compareTo(Integer.valueOf(s2.getExamScore()));
}
});
System.out.println("Students sorted by score in ascending order");
for(int j = 0; j < counter; j++){
System.out.println(studentArray[j]);
}
//To compute the average:
double sum = 0.0;
for(int j = 0; j < counter; j++){
sum+=studentArray[j].getExamScore();
}
double average = (sum*1.0)/counter;
System.out.println("Average Score = "+average );
aScanner.close();
}
或
这将比较两个值并相应地返回
编辑:
程序中的一些更正:
在Student
类中添加toString()
方法,如下所示:
@凌驾
公共字符串toString(){
返回firstName+“”+lastName+:“+examcore;
}
更新应用程序中的main()
方法,如下所示:
return (new Integer(examScore)).compareTo((new Integer(s.examScore));
public static void main(String[] args) throws Exception {
Scanner aScanner = new Scanner(
new FileReader("src//chapt11//ch11AS8data.txt"));
System.out.println("Welcome to the Student Scores Application.\n");
int nStudent = 100;
Student[] studentArray = new Student[nStudent];
Scanner lineScanner = null;
int counter = 0;
while (aScanner.hasNext()) {
lineScanner = new Scanner(aScanner.nextLine());
String lastName = lineScanner.next();
String firstName = lineScanner.next();
int examScore = lineScanner.nextInt();
System.out.println("Student " + counter + " " + firstName + " "
+ lastName + " " + +examScore);
studentArray[counter++]=new Student(lastName, firstName, examScore);
lineScanner.close();
}
for(int j = 0; j < counter; j++){
System.out.println(studentArray[j]);
}
//sort based on first name
Arrays.sort(studentArray,0,counter, new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
return s1.getFirstName().compareTo(s2.getFirstName());
}
});
System.out.println("Students sorted by first name in ascending order");
for(int j = 0; j < counter; j++){
System.out.println(studentArray[j]);
}
//sort based on score
Arrays.sort(studentArray,0,counter, new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
return Integer.valueOf(s1.getExamScore()).
compareTo(Integer.valueOf(s2.getExamScore()));
}
});
System.out.println("Students sorted by score in ascending order");
for(int j = 0; j < counter; j++){
System.out.println(studentArray[j]);
}
//To compute the average:
double sum = 0.0;
for(int j = 0; j < counter; j++){
sum+=studentArray[j].getExamScore();
}
double average = (sum*1.0)/counter;
System.out.println("Average Score = "+average );
aScanner.close();
}
publicstaticvoidmain(字符串[]args)引发异常{
扫描仪aScanner=新扫描仪(
新的文件阅读器(“src//chapt11//ch11AS8data.txt”);
System.out.println(“欢迎使用学生成绩应用程序。\n”);
int nStudent=100;
学生[]学生阵列=新学生[n学生];
Scanner lineScanner=null;
int计数器=0;
while(aScanner.hasNext()){
lineScanner=新扫描仪(aScanner.nextLine());
字符串lastName=lineScanner.next();
String firstName=lineScanner.next();
int examcore=lineScanner.nextInt();
System.out.println(“学生”+计数器+“”+名字+“”
+lastName++++examcore);
studentArray[counter++]=新学生(姓、名、ExamCore);
lineScanner.close();
}
对于(int j=0;j
因为这是家庭作业,所以我不会给你完整的答案…但这里是你应该努力的总体想法
import java.io.FileReader;
import java.util.Scanner;
public class App {
public static void main( String[] args ) throws Exception {
// TODO Auto-generated method stub
Scanner aScanner = new Scanner( new FileReader( "src/scores.txt" ) );
System.out.println( "Welcome to the Student Scores Application." );
System.out.println();
Student[] studentArray;
String lastName;
String firstName;
int examScore = 0;
double average = 0;
int nStudent = 100;
studentArray = new Student[nStudent];
int counter = 0;
while( aScanner.hasNext() ) {
lastName = aScanner.next();
firstName = aScanner.next();
examScore = aScanner.nextInt();
studentArray[counter] = new Student( lastName, firstName, examScore );
System.out.println( "Student " + studentArray[counter].getFirstName() + " " + studentArray[counter].getLastName() + "'s " + "Test Score is :" + studentArray[counter].getExamScore() );
++counter;
if( counter >= nStudent ) {
break;
}
}
}
static class Student implements Comparable<Student> {
private String firstName;
private String lastName;
private int examScore;
public Student( String firstName, String lastName, int examScore ) {
this.firstName = firstName;
this.examScore = examScore;
this.lastName = lastName;
}
// Get & Set Methods
public int getExamScore() {
return examScore;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public int compareTo( Student s ) {
if( !lastName.equalsIgnoreCase( s.lastName ) ) {
return lastName.compareToIgnoreCase( s.lastName );
}
if( !firstName.equalsIgnoreCase( s.firstName ) ) {
return firstName.compareToIgnoreCase( s.firstName );
}
return examScore - s.examScore;
}
}
}
导入java.io.FileReader;
导入java.util.Scanner;
公共类应用程序{
公共静态void main(字符串[]args)引发异常{
//TODO自动生成的方法存根
Scanner aScanner=newscanner(newfilereader(“src/scores.txt”);
System.out.println(“欢迎使用学生成绩应用程序”);
System.out.println();
学生[]学生阵列;
字符串lastName;
字符串名;
int examcore=0;
双平均=0;
int nStudent=100;
学生阵列=新生[学生];
int计数器=0;
while(aScanner.hasNext()){
lastName=aScanner.next();
firstName=aScanner.next();
examcore=aScanner.nextInt();
学生阵列[计数器]=新学生(姓、名、ExamCore);
System.out.println(“学生”+studentArray[counter].getFirstName()+“”+studentArray[counter].getLastName()+“'s”+”的测试分数为:“+studentArray[counter].getExamCore());
++计数器;
如果(计数器>=N学生){
import java.io.FileReader;
import java.util.Scanner;
public class App {
public static void main( String[] args ) throws Exception {
// TODO Auto-generated method stub
Scanner aScanner = new Scanner( new FileReader( "src/scores.txt" ) );
System.out.println( "Welcome to the Student Scores Application." );
System.out.println();
Student[] studentArray;
String lastName;
String firstName;
int examScore = 0;
double average = 0;
int nStudent = 100;
studentArray = new Student[nStudent];
int counter = 0;
while( aScanner.hasNext() ) {
lastName = aScanner.next();
firstName = aScanner.next();
examScore = aScanner.nextInt();
studentArray[counter] = new Student( lastName, firstName, examScore );
System.out.println( "Student " + studentArray[counter].getFirstName() + " " + studentArray[counter].getLastName() + "'s " + "Test Score is :" + studentArray[counter].getExamScore() );
++counter;
if( counter >= nStudent ) {
break;
}
}
}
static class Student implements Comparable<Student> {
private String firstName;
private String lastName;
private int examScore;
public Student( String firstName, String lastName, int examScore ) {
this.firstName = firstName;
this.examScore = examScore;
this.lastName = lastName;
}
// Get & Set Methods
public int getExamScore() {
return examScore;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public int compareTo( Student s ) {
if( !lastName.equalsIgnoreCase( s.lastName ) ) {
return lastName.compareToIgnoreCase( s.lastName );
}
if( !firstName.equalsIgnoreCase( s.firstName ) ) {
return firstName.compareToIgnoreCase( s.firstName );
}
return examScore - s.examScore;
}
}
}