【Java作业】2017.11.25 / 11.26

本文档包含了两个Java作业的说明。第一个作业要求读取s.txt文件中的字符串,将其排序后写入ss.txt。第二个作业涉及键盘录入5个学生的姓名及三门课程成绩,按总分降序存入文本文件。

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

作业一:已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”请编写程序读取数据内容,把数据排序后写入ss.txt中。


方式1:使用高效字符流,一次读取一个字符
/*
 * 需求:
 *  	已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
 *	 	请编写程序读取数据内容,把数据排序后写入ss.txt中
 * */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Test {
	public static void main(String[] args) throws IOException {
		// 创建字符输入流对象
		BufferedReader br = new BufferedReader(new FileReader("s.txt"));
		// 创建字符输出流对象
		BufferedWriter bw = new BufferedWriter(new FileWriter("ss.txt"));

		// 创建ArrayList集合对象
		ArrayList<Character> al = new ArrayList<Character>();

		// 使用高效字符输入流获取
		int ch = 0;
		while ((ch = br.read()) != -1) {
			// 添加至集合中
			al.add((char) ch);
		}

		// 遍历集合并排序
		for (int i = 0; i < al.size() - 1; i++) {
			for (int j = 0; j < al.size() - i - 1; j++) {
				char c1 = al.get(j);
				char c2 = al.get(j + 1);

				if (c1 > c2) {
					al.set(j, c2);
					al.set(j + 1, c1);
				}
			}
		}

		// 将集合转为Character型数组
		Character[] chs = al.toArray(new Character[al.size()]);

		for (int i = 0; i < chs.length; i++) {
			// 将Character对象转换为char类型
			char c = chs[i].charValue(); // public char charValue() 返回此对象的基本char值
			// 将字符使用输出流写入ss.txt文件中
			bw.write(c);
			// 刷新流
			bw.flush();
		}

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

	}
}



方法2:使用高效字符流,一次读取一行
/*
 * 需求:
 *  	已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
 *	 	请编写程序读取数据内容,把数据排序后写入ss.txt中
 * */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

public class Homework4 {
	public static void main(String[] args) throws IOException {
		// 创建字符输入流对象
		BufferedReader br = new BufferedReader(new FileReader("s.txt"));
		// 创建字符输出流对象
		BufferedWriter bw = new BufferedWriter(new FileWriter("ss.txt"));

		// 使用高效字符输入流获取
		String str = br.readLine();
		
		//转为字符数组
		char [] chs = str.toCharArray();
		
		//使用Arrays类中的sort()方法进行排序
		Arrays.sort(chs);
		
		//使用输出流写入ss.txt中
		bw.write(chs);
		//刷新流
		bw.flush();
		
		//释放资源
		br.close();
		bw.close();
			
	}
}


结果:















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


方式1:使用TreeSet集合的比较器排方法
/*
 * 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
 * */

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

public class Homework6 {
	public static void main(String[] args) throws IOException {

		// 创建TreeSet集合对象
		TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {

			@Override
			public int compare(Student s1, Student s2) {
				// 比较总分按从高到低
				int num1 = s2.getSum() - s1.getSum();
				// 若总分相同比较语文成绩
				int num2 = num1 == 0 ? s2.getChinese() - s1.getChinese() : num1;
				// 总分相同,语文成绩也相同则比较数学成绩
				int num3 = num2 == 0 ? s2.getMath() - s1.getMath() : num2;
				// 总分相同,所有科目成绩也相同,则比较姓名
				int num4 = num3 == 0 ? s2.getName().compareTo(s1.getName()) : num3;

				return num4;
			}

		});

		System.out.println("请录入学生信息:");
		// 创建键盘录入对象,分别录入五个学生的信息
		for (int i = 1; i <= 5; i++) {
			// 创建键盘录入对象
			Scanner sc = new Scanner(System.in);

			System.out.println("请输入第" + i + "位学生姓名:");
			String name = sc.nextLine();
			System.out.println("请输入第" + i + "位学生的语文成绩:");
			String chinese = sc.nextLine();
			System.out.println("请输入第" + i + "位学生的数学成绩:");
			String math = sc.nextLine();
			System.out.println("请输入第" + i + "位学生的英语成绩:");
			String english = sc.nextLine();

			// 将这些信息封装到学生对象中
			Student student = new Student();
			student.setName(name);
			student.setChinese(Integer.parseInt(chinese));
			student.setMath(Integer.parseInt(math));
			student.setEnglish(Integer.parseInt(english));

			// 将学生对象添加至集合中
			ts.add(student);
		}
		System.out.println("学生信息录入结束!");

		// 创建输出流对象
		BufferedWriter bw = new BufferedWriter(new FileWriter("学生成绩.txt"));

		/*
		 * 此种方式错误,不能用此种方式转换,否则会出现类转换异常
		 * 
		 * // 将TreeSet集合转为数组 Object[] objs = ts.toArray(); char[] chs = new char[1024];
		 * for (int i = 0; i < objs.length; i++) { chs[i] = (char) objs[i]; } //写入数据
		 * bw.write(chs); // 刷新流 bw.flush();
		 * 
		 */

		// 写入数据
		bw.write("五位学生的信息如下:");
		bw.newLine();
		bw.write("姓名\t\t\t语文\t\t数学\t\t英语");
		bw.newLine();
		// 刷新资源
		bw.flush();
		// 使用增强for遍历集合
		for (Student s : ts) {
			// 创建StringBuilder对象
			StringBuilder sb = new StringBuilder();
			sb.append(s.getName() + "\t\t").append(s.getChinese() + "\t\t").append(s.getMath() + "\t\t")
					.append(s.getEnglish());
			// 将StringBuilder对象转换为字符
			String str = sb.toString();
			// 用输出流写入
			bw.write(str);
			// 换行
			bw.newLine();
			// 刷新流
			bw.flush();
		}

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

	}
}

// 学生类
class Student {
	// 定义四个变量
	private String name;
	private int chinese;
	private int math;
	private int english;

	// 有参和无参构造
	public Student() {
		super();
	}

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

	// set和get方法获取变量
	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;
	}

}





方式2:使用TreeSet集合的自然排序方法
/*
 * 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
 * */

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

public class Homework6 {
	public static void main(String[] args) throws IOException {

		// 创建TreeSet集合对象
		TreeSet<Student> ts = new TreeSet<Student>();
		
		//提示信息
		System.out.println("请录入学生信息:");
		// 创建键盘录入对象,分别录入五个学生的信息
		for (int i = 1; i <= 5; i++) {
			// 创建键盘录入对象
			Scanner sc = new Scanner(System.in);

			System.out.println("请输入第" + i + "位学生姓名:");
			String name = sc.nextLine();
			System.out.println("请输入第" + i + "位学生的语文成绩:");
			String chinese = sc.nextLine();
			System.out.println("请输入第" + i + "位学生的数学成绩:");
			String math = sc.nextLine();
			System.out.println("请输入第" + i + "位学生的英语成绩:");
			String english = sc.nextLine();

			// 将这些信息封装到学生对象中
			Student student = new Student();
			student.setName(name);
			student.setChinese(Integer.parseInt(chinese));
			student.setMath(Integer.parseInt(math));
			student.setEnglish(Integer.parseInt(english));

			// 将学生对象添加至集合中
			ts.add(student);
		}
		System.out.println("学生信息录入结束!");

		
		// 创建输出流对象
		BufferedWriter bw = new BufferedWriter(new FileWriter("学生成绩.txt"));

		/*
		 * 此种方式错误,不能用此种方式转换,否则会出现类转换异常
		 * 
		 * // 将TreeSet集合转为数组 Object[] objs = ts.toArray(); char[] chs = new char[1024];
		 * for (int i = 0; i < objs.length; i++) { chs[i] = (char) objs[i]; } //写入数据
		 * bw.write(chs); // 刷新流 bw.flush();
		 * 
		 */

		// 写入数据
		bw.write("五位学生的信息如下:");
		bw.newLine();
		bw.write("姓名\t\t\t语文\t\t数学\t\t英语");
		bw.newLine();
		// 刷新资源
		bw.flush();
		// 使用增强for遍历集合
		for (Student s : ts) {
			// 创建StringBuilder对象
			StringBuilder sb = new StringBuilder();
			sb.append(s.getName() + "\t\t").append(s.getChinese() + "\t\t").append(s.getMath() + "\t\t")
					.append(s.getEnglish());
			// 将StringBuilder对象转换为字符
			String str = sb.toString();
			// 用输出流写入
			bw.write(str);
			// 换行
			bw.newLine();
			// 刷新流
			bw.flush();
		}

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

	}
}

// 学生类
class Student implements Comparable<Student> {
	// 定义四个变量
	private String name;
	private int chinese;
	private int math;
	private int english;

	// 有参和无参构造
	public Student() {
		super();
	}

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

	// set和get方法获取变量
	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;
	}

	@Override
	public int compareTo(Student s) {
		// 比较总分按从高到低
		int num1 = s.getSum() - this.getSum();
		// 若总分相同比较语文成绩
		int num2 = num1 == 0 ? s.getChinese() - this.getChinese() : num1;
		// 总分相同,语文成绩也相同则比较数学成绩
		int num3 = num2 == 0 ? s.getMath() - this.getMath() : num2;
		// 总分相同,所有科目成绩也相同,则比较姓名
		int num4 = num3 == 0 ? s.getName().compareTo(this.getName()) : num3;

		return num4;
	}

}

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值