懂java的朋友快来帮我看看这题
程序我都写了注释,把Scores。txt文件放在C盘根目录下就行了。
运行MainTest就可以测试了
//创建类,记录姓名及成绩
public class StudentScore {
private String name;
private Integer score;
//构造方法
public StudentScore() {
super();
}
public StudentScore(String name, Integer score) {
super();
this。 name = name;
this。score = score;
}
//get set 方法
publ...全部
程序我都写了注释,把Scores。txt文件放在C盘根目录下就行了。
运行MainTest就可以测试了
//创建类,记录姓名及成绩
public class StudentScore {
private String name;
private Integer score;
//构造方法
public StudentScore() {
super();
}
public StudentScore(String name, Integer score) {
super();
this。
name = name;
this。score = score;
}
//get set 方法
public String getName() {
return name;
}
public void setName(String name) {
this。
name = name;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this。
score = score;
}
}
import java。util。Comparator;
public class Stu_Score_Comparator implements Comparator{
//比较器,创建排序规则,用来进行降序排序
public int compare(Object o1, Object o2) {
StudentScore score1=(StudentScore)o1;
StudentScore score2=(StudentScore)o2;
//成绩降序
return score2。
getScore()-score1。getScore();
}
}
import java。io。BufferedReader;
import java。io。File;
import java。
io。FileNotFoundException;
import java。io。FileReader;
import java。io。IOException;
import java。
io。Reader;
import java。util。ArrayList;
import java。util。Collections;
import java。util。List;
public class Score {
List stuList = new ArrayList();//学生成绩列表
Integer counter=0;//统计未及格数量
Stu_Score_Comparator com=new Stu_Score_Comparator();//比较器
public void readFile(String filePath){
System。
out。println(filePath);
File file =new File(filePath);
Reader fis;
try {
fis = new FileReader(file);
BufferedReader reader = new BufferedReader(fis);
String value;
while((value=reader。
readLine())!=null){
int p=value。indexOf(":");
String name=value。substring(0,p)。trim();
Integer score = Integer。
parseInt(value。substring(p+1,value。length())。trim());
stuList。add(new StudentScore(name,score));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e。
printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e。
printStackTrace();
}
}
public void sortData(){
//按照比较器的规则进行排序
Collections。
sort(stuList,com);
}
public void displayInfo(){
for(StudentScore ){
System。out。println("姓名:"+temp。
getName()+" 成绩:"+temp。getScore());
}
}
public void calNoPass(){
counter=0;
for(StudentScore ){
if(temp。
getScore()<60)counter++;
}
System。out。println("未及格人数:"+counter);
}
}
public class MainTest {
public static void main(String args[]){
Score score = new Score();
score。
readFile("c:/Score。txt");
System。out。println("排序前");
score。displayInfo();
System。out。println("排序后");
//排序
score。
sortData();
score。displayInfo();
//不及格人数
score。calNoPass();
}
}
。收起