首页 > 其他分享 >集合IO流

集合IO流

时间:2024-08-17 21:38:37浏览次数:4  
标签:name chinese int IO english 集合 public math

package com.shujia.day17.ketang;



/*
        键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件

 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class IOTest3 {
    public static void main(String[] args) throws IOException {
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        //创建TreeSet集合对象
        TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //主要条件:总分从高到低
                int i = o2.getSumScore() - o1.getSumScore();
                //隐含条件
                //总成绩一样,语文成绩不一定一样
                int i2 = (i == 0) ? o2.getChinese() - o1.getChinese() : i;
                //语文成绩一样,数学成绩不一定一样
                int i3 = (i2 == 0) ? o2.getMath() - o1.getMath() : i2;

                //姓名不一定一样
                int i4 = (i3 == 0) ? o2.getName().compareTo(o1.getName()) : i3;
                return i4;
            }
        });

        Student student = null;
        for (int i = 1; i <= 5; i++) {
            System.out.println("请输入第" + i + "个学生的信息:");
            System.out.println("请输入姓名:");
            String name = sc.next();
            System.out.println("请输入语文成绩:");
            int chinese = sc.nextInt();
            System.out.println("请输入数学成绩:");
            int math = sc.nextInt();
            System.out.println("请输入英语成绩:");
            int english = sc.nextInt();
            student = new Student(name, chinese, math, english);
            set.add(student);
            System.out.println("---------------------------------------------");
        }
        StringBuffer sb = null;
        //创建字符缓冲输入流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("studentInfo.txt"));

        //遍历集合得到每个学生信息,将学生信息组成字符串写入到文件中
        for(Student student1 : set){
            sb = new StringBuffer();
            String name = student1.getName();
            int chinese = student1.getChinese();
            int math = student1.getMath();
            int english = student1.getEnglish();
            int sumScore = student1.getSumScore();
            sb.append("姓名:")
                    .append(name)
                    .append(",语文成绩:")
                    .append(chinese)
                    .append(",数学成绩:")
                    .append(math)
                    .append(",英语成绩:")
                    .append(english)
                    .append(",总成绩:")
                    .append(sumScore);
            //将StringBuffer转成字符串
            String s = sb.toString();
            bw.write(s);
            bw.newLine();
            bw.flush();
        }

        System.out.println("所有学生信息录入存储完毕!");


        //释放资源
        bw.close();


    }
}

student类

package com.shujia.day17.ketang;



public class Student {
    private String name;
    private int chinese;
    private int math;
    private int english;

    public Student() {
    }

    public Student(String name, int chinese, int math, int english) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    public String getName() {
        return name;
    }

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

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    public int getSumScore() {
        return chinese + math + english;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", chinese=" + chinese +
                ", math=" + math +
                ", english=" + english +
                '}';
    }
}

标签:name,chinese,int,IO,english,集合,public,math
From: https://www.cnblogs.com/qiwei-bigdata/p/18365017

相关文章

  • Java集合框架
    Java集合框架(JavaCollectionsFramework)是Java提供的一套用于表示和操作集合的统一架构,它位于java.util包中。集合框架包含了一系列的接口和类,用于存储和操作对象集合。这些接口和类之间的关系通过继承和多态性来实现,使得开发者能够以统一的方式操作不同的集合类型。Java集......
  • Axios
    1.Axios1.1异步请求发展史1.1.1传统的Ajax传统的Ajax请求是基于XMLHttpRequest(XHR)对象。可以直接使用。但 是使用起来配置较为麻烦,实际开发中使用非常少,在MVC时代通常使用的 是JQuery-Ajax。相对于传统的Ajax现在使用更多的是Fetch请求。1.1.2JQuery-AjaxJQuery-Ajax在前......
  • P5642 人造情感(emotion)
    link考虑\(W(U)\)怎么求。定义\(f_x\)表示只考虑所有在\(x\)子树内的路径时最大收益,\(sum_x\)为只考虑\(x\)子树中路径,且钦定\(x\)不选的最大收益。\(g\)的转移显然:\(g_x=\sumf_{to}\)\(f\)转移考虑枚举\(\text{lca}=x\)的所有路径\((u,v,w)\),有:\(f_x\longle......
  • 登录 k8s-Dashboard 显示 Your connection is not private
    目录一、背景二、解决方案一、背景部署好kubernetes-Dashboard后使用master节点的ip+port登录Dashboard显示Yourconnectionisnotprivate无论是Edge还是GoogleChrome都是这样的情况二、解决方案点击网页空白处,英文输入法输入:thisisunsafe即可正常访问......
  • VisionPro连接相机
    相机硬件接口介绍PWR状态灯 常规工业相机电源线介绍搜索CognexGigEVisionConfigura...修改子网掩码为255.255.255.0配置驱动程序更新驱动(如果能够选择9014Bytes,跳过此步骤)更新更改相机ip配置打开visionPro选择照相机查看实时画面运行......
  • Self-Attention自注意力机制解读(2):图解版!
    文章目录一、前言二、流程解读1.它整体做了一件什么事2.多层Self-attention3.self-attention做了一件什么事4.具体流程三、流程的矩阵表示三、Softmax层的解释一、前言上一篇文章Self-Attention自注意力机制:深度学习中的动态焦点|手把手实例解析看不懂你打我以......
  • SciTech-BigDataAIML-LLM-Transformer Series-Self-Attention:由Dot-Product(向量点乘)
    SelfAttention:由Dot-Product(向量点乘)说起https://lulaoshi.info/deep-learning/attention/transformer-attention.html#self-attention-从向量点乘说起Transformer[1]论文提出了一种Self-Attention(自注意力机制),Self-Attention的最核心的公式为:\(\large\begin{align*}......
  • application.yml文件配置springboot项目
    基本用法#注意空格都不能省#配置端口号server:port:8080address:127.0.0.1#配置数据库spring:datasource:driver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/tliasusername:rootpassword:root#定义对象/Ma......
  • 《python语言程序设计》2018版第7章第06题代数:平方根 设计一个名为QuadraticEquation
    类代码部分classQuadraticEquation:def__init__(self,a,b,c):self.a=aself.b=bself.c=cdefset_a(self,a):self.a=adefget_a(self):returnself.adefset_b(self,b):self......
  • pdf转png有哪些免费方法?集合6个在线pdf转图片软件,错过就可惜了!
    有时,很多小伙伴们需要将pdf文件转换成png格式,以便更方便地分享、编辑其中的图片、进行网页设计等多种用途。网上有许多工具可以实现pdf转png。那么,选择哪款软件或工具能够确保高质量的在线pdf转图片呢?此外,是否可以将多个pdf页面一次性都转换成图片呢?在本文中,我们将详细介绍6款......