楼主,稍微优化了一下代码,你自己调试一下。
输出结果:
编号:1 姓名:mm 最后得分:48.0 平均分:3.2 名次:1
编号:1 姓名:kk 最后得分:47.0 平均分:3.1333334 名次:2
编号:1 姓名:gg 最后得分:36.0 平均分:2.4 名次:3
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;
public class SingleDemo {
public static void main(String[] args) {
showAll(sortAll(readAll("C:\\Documents and Settings\\xxx\\桌面\\tt.txt")));
}
// 读入文件中所有歌手的信息
private static ArrayListreadAll(String path) {
BufferedReader reader = null;
ArrayListlist = new ArrayList ();
try {
File file = new File(path);
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
StringTokenizer tokenizer = new StringTokenizer(line, "|**|");
int count = 1;
Singer singer = new Singer();
while (tokenizer.hasMoreTokens()) {
String tempValue = tokenizer.nextToken();
if (count == 1) singer.setId(Integer.parseInt(tempValue));
if (count == 2) singer.setName(tempValue);
if (count == 3) {
String[] tempScore = tempValue.split(",");
float[] scores = new float[15];
for (int i = 0; i < tempScore.length; i++) {
scores[i] = Float.valueOf(tempScore[i]);
}
singer.setScore(scores);
}
count++;
}
list.add(singer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null) reader.close();
reader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
// 实现对所有歌手按平均分降序排序
private static ArrayListsortAll(ArrayList list) {
Collections.sort(list);
return list;
}
// 输出所有选手信息(包括编号、姓名、最后得分、名次)
private static void showAll(ArrayListlist) {
Iteratorit = list.iterator();
int n = 1;
while (it.hasNext()) {
Singer singer = it.next();
System.out.println("编号:" + singer.getId() + "\t姓名:" + singer.getName() + "\t最后得分:" + singer.getLastScore()
+ "\t平均分:" + singer.getAverage() + "\t名次:" + n);
n++;
}
}
}
class Singer implements Comparable{
private int id;
private String name;
private float[] score;
// 求总成绩
public float getLastScore() {
float count = 0;
for (float i : score) {
count += i;
}
return count;
}
// 求平均值
public float getAverage() {
return getLastScore() / score.length;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float[] getScore() {
return score;
}
public void setScore(float[] score) {
this.score = score;
}
@Override
public int compareTo(Singer s) {
return this.getLastScore() > s.getLastScore() ? -1 : this.getLastScore() == s.getLastScore() ? 0 : 1;
}
}
public double averscore(Singer singer)
{
int sum = 0,max=0,min=0;
for(int i=0;isum+=singer.getScore()[i];
if(maxif(min>singer.getScore()[i]) min=singer.getScore()[i];
}
return (sum-max-min)/3;
}
这个是平均分的方法
public class Singer {
private int id;
private String name;
private int[] score = new int[15];
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int[] getScore() {
return score;
}
public void setScore(int[] score) {
this.score = score;
}
}
这个是实体类
readAll读从哪读?
很明显的错误,一个类中只能有一个public修饰的类,你的类中有两个,肯定不对,你先修改这个,如果还有问题,将其提出来并附上需要的结果或者实现的功能……
你在吗?如果你还在线的话,我可以帮你调试一下。如果不在我就不回复了。