IO流_键盘录入学生信息按照总分排序并写入文本文件案例

101 篇文章 0 订阅
85 篇文章 0 订阅

学生类

package cn.itcast_06;

public class Student {
	// 姓名
	private String name;
	// 语文
	private int chinese;
	// 数学
	private int math;
	// 英语
	private int english;

	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Student(String name, int chinese, int math, int english) {
		super();
		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 getSum() {
		return this.chinese + this.math + this.english;
	}
}

测试类

package cn.itcast_06;

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

/*
 * 需求:键盘录入5个学生信息(姓名,语文,数学,英语),按照总分从高到低存入文本文件
 * 
 * 分析:
 * 		A:创建学生类
 * 		B:创建集合对象
 * 			TreeSet(Student)
 * 		C:键盘录入学生信息存储到集合
 * 		D:遍历集合,把数据写到文本文件
 */
public class StudentDemo {
	public static void main(String[] args) throws IOException {
		// 创建集合对象
		TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
			public int compare(Student s1, Student s2) {
				int num = s2.getSum() - s1.getSum();
				System.out.println(s2.getSum()+"---"+s1.getSum());
				int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
				int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
				int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;
				int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName())
						: num4;
				return num5;
			};
		});

		// 键盘录入学生信息存储到集合
		for (int x = 1; x <= 2; x++) {
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入第" + x + "个学生信息");
			System.out.print("姓名:");
			String name = sc.nextLine();
			System.out.print("语文:");
			int chinese = sc.nextInt();
			System.out.print("数学:");
			int math = sc.nextInt();
			System.out.print("英语:");
			int english = sc.nextInt();

			Student s = new Student();
			s.setName(name);
			s.setChinese(chinese);
			s.setMath(math);
			s.setEnglish(english);

			ts.add(s);
		}

		// 遍历集合,把数据写到文本文件
		BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));

		// 写出数据
		bw.write("学生信息如下:");
		bw.newLine();
		bw.flush();
		bw.write("姓名,语文成绩,数学成绩,英语成绩");
		bw.newLine();
		bw.flush();
		for (Student s : ts) {
			StringBuilder sb = new StringBuilder();
			sb.append(s.getName()).append(",").append(s.getChinese())
					.append(",").append(s.getMath()).append(",")
					.append(s.getEnglish());

			bw.write(sb.toString());
			bw.newLine();
			bw.flush();

		}
		bw.close();
		System.out.println("学生信息存储完毕");

	}
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值