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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ouyzm 中级黑马   /  2016-10-22 00:31  /  680 人查看  /  1 人回复  /   4 人收藏 转载请遵从CC协议 禁止商业使用本文

package zuoye;


import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class ZuoYe_01 {

        /**
         * 需求:随机生成10个1-100(包含100)之间的不重复随机整数,把其中的奇数并且是3的倍数(可包含3)按从大到小排序(要求不能重复),
然后按照指定格式打印到当前工程项目下的num.txt文本中
* 例如:99, 81, 75, 57, 39, 33, 27, 21, 15, 3 (注意:最后一个数字没有逗号)
* 判分标准:完成需求则满分
         * @throws IOException

         */
        public static void main(String[] args) throws IOException {
                Random r = new Random();
                ArrayList<Integer> list = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                        int x = r.nextInt(100)+1;
                        if (x%2==1&&(x%3==0)&&!(list.contains(x))) {
                                list.add(x);
                        }
                }
                StringBuffer s = new StringBuffer();
                Integer[] arr = list.toArray(new Integer[0]);
                for (int i = 0; i < arr.length; i++) {
                        for (int j = i+1; j < arr.length; j++) {
                                if (arr[i]<arr[j]) {
                                        int temp = arr[i];
                                        arr[i] = arr[j];
                                        arr[j] = temp;
                                }
                               
                                }
                        if (i==list.size()) {
                                s.append(arr[i]);
                        }else {
                                s.append(arr[i]+",");
                        }
                }
                String st = new String(s);
                byte[] bytes = st.getBytes();
                FileOutputStream fos = new FileOutputStream("num.txt");
                fos.write(bytes);
               
                fos.close();
        }

}


1 个回复

倒序浏览
[AppleScript] 纯文本查看 复制代码
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {

            @Override
            public int compare(Integer lhs, Integer rhs) {
                return rhs.compareTo(lhs);
            }
        });
        while (ts.size() < 10) {
            int num = (int) (Math.random() * 100 + 1);
            if ((num & 1) != 0 && num % 3 == 0) {
                ts.add(num);
            }
        }
        
        StringBuilder sb = new StringBuilder();
        int index = 0;
        for (Integer value : ts) {
            index++;
            if (index != ts.size()) {
                sb.append(value + ",");
            } else {
                sb.append(value);
            }
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter("num.txt"));
        bw.write(sb.toString());
        bw.close();
    }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马