java数组学生名字成绩数组,使用compareTo方法与数组学生的名字和考试成绩排序...

这篇博客讨论了如何在Java应用中读取文本文件中的学生名单和成绩,按姓名和分数排序,并计算平均分。博主遇到的问题在于实现Comparable接口和比较方法以完成排序,同时还要在低于平均分15分的学生旁显示提示信息。目前,博主已能读取和打印数据,但排序和条件判断部分尚未完成。
摘要由CSDN通过智能技术生成

I need to use the comparable interfact and a compareTo method to sort a list of students alphabetically and then by test score. I'm struggling at getting this to work in my application.

The list of names needs to be read from a text file. I don't know how many names will be in the text file that my professor uses except that it will be less than 100. I am also supposed to display the average of the grades at the bottom as well as write a message next to any student that is 15 points below average. I have not really gotten to the writing message part as I am currently stuck on getting the names and scores to print and sort.

The text file should look something like this:

Steelman Andrea 95

Murach Joel 98

Lowe Doug 82

Murach Mike 93

This is what I have so far... if someone could give me a little direction I'd appreciate it. Thank you.

package chapt11;

import java.io.FileReader;

import java.util.Arrays;

importjava.util.Scanner;

public class CH11AS8App {

/**

* @param args

*/

public static void main(String[] args) throws Exception {

System.out.println("Welcome to the Student Scores Application.");

System.out.println();

Scanner aScanner = new Scanner(new FileReader(

"src//chapt11//ch11AS8data.txt"));

Student [] studentArray;

String lastName;

String firstName;

int examScore = 0;

double average = 0;

int nStudent = 100; //array size not set unit run time

studentArray = new Student[nStudent];

for (int i=0; i

{

System.out.println();

while (aScanner.hasNext()) {

lastName = aScanner.next();

firstName = aScanner.next();

examScore = aScanner.nextInt();

System.out.println("Student " + nStudent++ + " " + firstName

+ " " + lastName + " " + +examScore);

studentArray[i] = new Student(lastName, firstName, examScore);

}

double sum = 0.0;

sum += examScore;

average = sum/nStudent;

Arrays.sort(studentArray);

System.out.println();

for (Student aStudent: studentArray)

{

System.out.println(aStudent);

if (examScore<= (average-10))

{

System.out.println ("Score 10 points under average");

}

System.out.println("Student Average:" +average);

}

}

}

public interface Comparable {

int compareTo(Object o);

}

class Student implements Comparable {

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(Object o) {

Student s = (Student) o;

if (s.lastName.equals(lastName)) {

return firstName.compareToIgnoreCase(s.firstName);

} else {

return lastName.compareToIgnoreCase(s.lastName);

}

}

public String toString() {

return lastName + ", " + firstName + ": " + examScore;

}

}

}

解决方案

Firstly, delete entirely your Comparable interface. Use Comparable from the JDK.

Change your code to this

public static class Student implements Comparable {

// rest of class omitted

@Override

public int compareTo(Student s) {

if (s.lastName.equals(lastName)) {

return firstName.compareToIgnoreCase(s.firstName);

}

return lastName.compareToIgnoreCase(s.lastName);return

}

}

Note also that there is no "else" after a conditional return, so I omitted that redundant part of the code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值