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

© shizhensuiyue 中级黑马   /  2016-12-26 10:52  /  1162 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1.编写一个方法,将1+2!+3!+...+20!的和存入到当前项目根目录number.txt中。(例如:3!=3*2*1         5!= 5*4*3*2*1)
2. 模拟抢红包过程,生成5个红包(金额是随机生成的,范围在1-10元之间),创建5个线程代表5个人,然后让这5个人去抢这5个红包,每次抢红包需要300ms的时间,在控制台打印出 (xxx抢了xxx元)(不限定每人抢的次数并且抢到红包后还可以接着抢,每次生成一个红包)

4 个回复

倒序浏览
        [mw_shl_code=java,true]public class Demo1 {

        public static void main(String[] args) throws IOException {
                //计算结果
                long num = 0;
                //创建对象
                FileOutputStream foss = new FileOutputStream(new File("number.txt"));
                //循环1~20的数
                for (int i = 1; i < 21; i++) {
                        //累加每次计算的结果
                        num = num + count(i);
                }
                //将long型转换成string型
                String str = String.valueOf(num);
                //将string转换成char数组
                char[] numarr = str.toCharArray();
                //遍历数组
                for (int i = 0; i < numarr.length; i++) {
                        //写在文件上
                        foss.write((int)numarr);
                }
                //关闭流
                foss.close();
        }
        //创建阶乘方法
        public static long count(int c) {
                //阶乘结果
                long ride = 1;
                //遍历每个数
                for (int i = c; i > 0; i--) {
                        //计算阶乘,没一个数逐次减一相乘
                        ride = ride * i;
                       
                }
                //返回阶乘数
                return ride;
               
        }
       
}[/mw_shl_code]
}
回复 使用道具 举报
[mw_shl_code=java,true]public class Demo2 {
        public static void main(String[] args) {
                //开启1线程
                new mythread("老大").start();
                //开启2线程
                new mythread("老二").start();
                //开启3线程
                new mythread("老三").start();
                //开启4线程
                new mythread("老四").start();
                //开启5线程
                new mythread("老五").start();
        }

}

class mythread extends Thread {
        //设置共享变量
        private static int packet = 5;
        //有参构造方法
        public mythread(String name) {
                //调用父类设置姓名
                super(name);
        }
        //重写run方法
        public void run() {
                //创建循环
                while (true) {
                        //同步代码
                        synchronized (Thread.class) {
                                //判断抢红包次数
                                if (packet <= 0) {
                                        //退出循环
                                        break;
                                }
                                //异常
                                try {
                                        //300ms延时
                                        Thread.sleep(300);
                                //异常
                                } catch (InterruptedException e) {
                                       
                                        e.printStackTrace();
                                }
                                //获取范围在1-10元之间,随即钱数
                                int money = (int)(Math.random()*10+1);
                                //开抢
                                System.out.println(getName() + "抢了" + money + "元");
                                //抢完红包数减一
                                packet--;
                        }
                }
        }
}[/mw_shl_code]
回复 使用道具 举报
感谢给我指正
回复 使用道具 举报
干嘛不用字符流直接写
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马