黑马程序员技术交流社区
标题:
求最后一步代码,实在写不出来了
[打印本页]
作者:
tujiu888
时间:
2016-2-17 21:14
标题:
求最后一步代码,实在写不出来了
将用户在控制台上输出的5个数字按照大小顺序存入到D:\\xx.txt中并以相反的的顺序读出显示在控制台上
public class Test1 {
public static void main(String[] args) throws IOException {
//BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\xx.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("xx.txt"));
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
TreeSet<Integer> ts = new TreeSet<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
int num = i2.compareTo(i1);
return num == 0 ? 1 : num;
}
});
while(ts.size() < 5) {
try {
String line = sc.nextLine();
Integer i = Integer.parseInt(line);
ts.add(i);
} catch (Exception e) {
System.out.println("类型输入错误,请重新输入");
}
}
Iterator<Integer> it = ts.iterator();
while(it.hasNext()) {
Integer i = it.next();
//String string = i.toString();
bw.write(i.toString() + " ");
bw.flush();
}
bw.close();
求最后一步输出代码 实在写不出来也没有什么思路了
}
}
作者:
j6819236
时间:
2016-2-17 21:44
public class Test1 {
public static void main(String[] args) throws IOException {
// BufferedOutputStream bos = new BufferedOutputStream(new
// FileOutputStream("D:\\xx.txt"));
File file = new File("D:/xx.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
TreeSet<Integer> ts = new TreeSet<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
int num = i2.compareTo(i1);
return num == 0 ? 1 : num;
}
});
while (ts.size() < 5) {
try {
String line = sc.nextLine();
Integer i = Integer.parseInt(line);
ts.add(i);
} catch (Exception e) {
System.out.println("类型输入错误,请重新输入");
}
}
bw.write(ts.toString());
bw.close();
BufferedReader br=new BufferedReader(new FileReader(file));
String line=br.readLine();
StringBuffer sb=new StringBuffer(line.substring(1, line.length()-1));
sb=sb.reverse().insert(0, "[").append("]");
System.out.println(sb);
br.close();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2