昨晚我发布了一个关于这个的问题,但我仍然在苦苦挣扎。我需要先从文本文件中按名称(使用此格式的姓氏,名字),然后按测试分数对学生列表进行排序。我不知道有多少学生会在文本文件中,除了少于100.我必须使用compareTo方法(我认为我做得正确)和一个数组(我不认为我'我正确使用这个)。几个小时以来,我一直在搞乱这个问题,而我似乎并没有得到它。教科书真的似乎没有帮助我。如何让我的app类打印已排序的学生姓名/成绩?
其他注意事项是获得分数的平均值,并在低于平均值的任何分数旁边发表评论。然而,在我能够完成排序之后,我可以继续努力。
这是我的代码......
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; }
}
}
答案 0 :(得分:4)
您的错误出现在compareTo()
方法的最后一行:
变化
return examScore = s.examScore;
到
return examScore - s.examScore;
您想要返回差异,而不是设置实例变量!
答案 1 :(得分:1)
我认为您应该在compareTo中更改最后一行,如下所示:
return (new Integer(examScore)).compareTo((new Integer(s.examScore));
或
return Integer.valueOf(examScore).compareTo(Integer.valueOf(s.examScore));
这将比较两个值并相应地返回。
修改强>
您的计划中的一些更正:
在toString()
课程中添加Student
方法:
@覆盖 public String toString(){ return firstName +“”+ lastName +“:”+ examScore; }
更新应用中的main()
方法,如下所示:
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();
}
答案 2 :(得分:0)
由于这是作业,我没有给你完整的答案......但是这里有你应该努力的一般想法...
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;
}
}
}