package com.itheima;
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个数字,按照降序存入到“D:\\number.txt”中
*/
public class Test11 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
int num = i2.compareTo(i1);
return num == 0 ? 1 : num;
}
});
// 请输入5个数字
for (int i = 1; i <= 5; i++) {
System.out.println("请输入第" + i + "个数字:");
int num = sc.nextInt();
ts.add(num);
}
BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\numbwe.txt"));
for (Integer integer : ts) {
bw.write(integer + "");
bw.newLine();
}
bw.close();
}
} |
|