黑马程序员技术交流社区

标题: 点招综合题,冲刺点招了 [打印本页]

作者: ouyzm    时间: 2016-10-22 00:31
标题: 点招综合题,冲刺点招了
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();
        }

}



作者: yu244934256    时间: 2016-10-22 00:35
[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();
    }





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2