A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 山雨欲来风满楼 中级黑马   /  2016-9-17 12:35  /  575 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


将用户在控制台上输入5个数字,按照降序存入到“D:\\number.txt”中。这道题不会做,求大神解答!感谢

1 个回复

倒序浏览
楼主,这个题很简单的 ,多练练,下面是我写的代码,你看看.我没写注释.你应该知道思路了,可以自己用别的方法谢谢.
先要有思路;
//将用户在控制台上输入5个数字,按照降序存入到“D:\\number.txt”中
public class Demo3 {
        public static void main(String[] args) throws IOException {
                Scanner sc = new Scanner(System.in);
                TreeSet<Integer> tr = new TreeSet<>( new Comparator<Integer>() {
                        public int compare(Integer i0, Integer i1) {
                                int num = i1 -i0;
                                return num == 0? 1 :num;
                        }
                       
                });
                while(tr.size()<5) {
                        System.out.println("请输入数字:");
                        int a = sc.nextInt();
                        tr.add(a);
                }
                FileOutputStream fis = new FileOutputStream("D:\\number.txt");
                for (Integer integer : tr) {
                        fis.write((integer+"").getBytes());
                        fis.write("\t".getBytes());
                }
                fis.close();
        }

}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马