第一次实验课作业(完成一个程序,程序功能是从班级成绩中选择分数最高的前N位同学。)

完成一个程序,程序功能是从班级成绩中选择分数最高的前N位同学。
具体要求如下:
1、使用java或python编程语言实现;
2、输入数据可以位txt或csv格式,三个字段分别为:姓名,学号,数据结构成绩;
3、定义topN函数,该函数的形式为int TopN(inputdir,outputpath)。函数调用成功返回1,失败返回0;inputdir为输入数据所在文件夹(可以处理多个输入文件);outputpath为输出结果保存路径
4、在linux环境下使用命令行编译源代码,生成可执行文件****.jar
5、使用命令行方式运行该jar测试验证代码
6、可以现使用eclipse等工具编辑测试代码,最后转移到linux环境。

package Java.ssy.Learning;
import java.util.*;
import java.io.*;
class MyStudent{
    String name;
    String id;
    Float grade;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Float getGrade() {
        return grade;
    }

    public void setGrade(Float grade) {
        this.grade = grade;
    }

    public MyStudent(){}
    public MyStudent(String name,String id,Float grade){
        this.name=name;
        this.id=id;
        this.grade=grade;
    }


}
public class MytopN {
     static int TOPN=3;
     static int getTopN(String input,String output){

         //读入文件
         String inputDirs[]=input.split(",");
         List<MyStudent> stuList=new ArrayList<>();
         try {
             for(String inputdirs:inputDirs){
                 FileInputStream fileInputStream=new FileInputStream(new File(inputdirs));
                 BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream));
                 String line;
                while((line=bufferedReader.readLine())!=null){
                     MyStudent stu=new MyStudent();
                     String stuInfo[]=line.split(" ");
                     stu.setName(stuInfo[0].trim());
                     stu.setId(stuInfo[1].trim());
                     stu.setGrade(new Float(stuInfo[2].trim()));
                     stuList.add(stu);
                 }
                 bufferedReader.close();
                fileInputStream.close();
             }
         } catch (FileNotFoundException e) {
             e.printStackTrace();
             return 0;
         }catch(IOException e){
             e.printStackTrace();
             return 0;
         }

         Collections.sort(stuList, new Comparator<MyStudent>() {
             @Override
             public int compare(MyStudent o1, MyStudent o2) {
                 return o2.getGrade().compareTo(o1.getGrade());//排序:o2.compareTo(o1)是升序,o1.compareTo(o2)是降序
             }
         });

         //文件输出

         try {
             FileOutputStream fileOutputStream=new FileOutputStream(output+"/topNStudent.txt");
             BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(fileOutputStream));
             bufferedWriter.write("成绩前"+TOPN+"的学生信息如下:\n");
             bufferedWriter.write("姓名 学号 成绩\n");
             for(int i=0;i<TOPN;i++){
                 bufferedWriter.write(stuList.get(i).getName()+" "+stuList.get(i).getId()+" "+stuList.get(i).getGrade()+"\n");
             }
             bufferedWriter.flush();
             fileOutputStream.close();
         } catch (FileNotFoundException e) {
             e.printStackTrace();
             return 0;
         }catch (IOException e){
             e.printStackTrace();
             return 0;
         }


         return 1;
     }
    public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入要读入的文件路径:");
        String input=scan.nextLine().trim();
        System.out.println("请输入要保存的文件路径:");
        String output=scan.nextLine().trim();
        int ans=getTopN(input,output);
        if(ans==1){
            System.out.println("已经成功查出前"+TOPN+"位同学的成绩");
        }
        else{
            System.out.println("查询失败");
        }
    }
}
//D:\siTest\BigDatatopN\Student.txt
//D:\siTest\BigDatatopN
//打包并在linux上运行,在另外的一篇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值