一个IO应用--查询txt文件中的top1

本文介绍了一个Java程序,用于读取两个文本文件:testscore.txt和testinfo.txt,根据输入的学院和专业,筛选并输出分数最高的学生姓名。程序实现了通过输入学院和专业的信息,从两个文件中获取对应学生的信息,并计算总分,从而找出最高分的学生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有两个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("计算机学院", "计算机科学与技术"));
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值