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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 悦鹏 中级黑马   /  2015-6-16 23:08  /  249 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class HuoQu_Random {
    public static void main(String[] args) {
                ArrayList nb=new ArrayList();
                while(nb.size()<10){
                         int x=new Random().nextInt(21);
                            if(nb.contains(x)){
                                   
                            }else  nb.add(x);       
                }
                System.out.println(nb);
                Iterator nb1=nb.iterator();
                while(nb1.hasNext()){
                 Object nb2=nb1.next();
                 System.out.print(nb2+"  ");
                }
        }
}

2 个回复

倒序浏览
牛奋 来自手机 中级黑马 2015-6-16 23:14:12
沙发
直接使用random()方法×21,简单的一逼。
回复 使用道具 举报
正好学到集合, 写的完全正确
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马