栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第一次实验课作业(完成一个程序,程序功能是从班级成绩中选择分数最高的前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 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() {
             @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 
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322350.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号