有两个txt文件testscore.txt和testinfo.txt,内容如下
testscore.txt
1001 计算机网络 90
1001 数据结构 95
1002 数据结构 98
1001 操作系统 16
1003 计算机网络 94
.......
testinfo.txt
1001 计算机学院 计算机科学与技术 FT
1002 软件 软件开发 小强
1003 计算机学院 计算机科学与技术 小Fire
......
功能要求:写一个程序,要求输入学院和学生所在系,能输出分数最高学生的姓名,挂科的不算。
Java 的一种实现如下:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TestIO {
FileReader fscore;
FileReader finfo;
BufferedReader bins;
BufferedReader bini;
TestIO() {
try {
fscore = new FileReader("testscore.txt");
finfo = new FileReader("testinfo.txt");
bins = new BufferedReader(fscore);
bini = new BufferedReader(finfo);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
double totelScore(String id) throws IOException {
double sum = 0;
String str = bins.readLine();
while ((null != str) && (str.length() > 0)) {
String[] sc = str.split("\\s+");
if ((sc.length > 0) && (id.equals(sc[0]))) {
double score = Double.valueOf(sc[2]);
if (score < 60) {
return -1;
}
sum += score;
}
str = bins.readLine();
}
return sum;
}
String getName(String pat, String pro) throws IOException {
double max = 0;
String name = " ";
String str = bini.readLine();
while ((null != str) && (str.length() > 0)) {
String[] stin = str.split("\\s+");
if ((stin.length > 0) && (pat.equals(stin[1]))
&& (pro.equals(stin[2]))) {
double temp = totelScore(stin[0]);
if (temp > max) {
max = temp;
name = stin[3];
}
}
str = bini.readLine();
}
return name;
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
TestIO test = new TestIO();
System.out.println(test.getName("计算机学院", "计算机科学与技术"));
}
}
本文介绍了一个Java程序,用于读取两个文本文件:testscore.txt和testinfo.txt,根据输入的学院和专业,筛选并输出分数最高的学生姓名。程序实现了通过输入学院和专业的信息,从两个文件中获取对应学生的信息,并计算总分,从而找出最高分的学生。
6558

被折叠的 条评论
为什么被折叠?



