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

package exam;

/*   
写一个程序,获取10个1至20的随机数,要求随机数不能重复
*/
public class D002 {

        public static void main(String[] args) {

                // 定义10随机数
                int temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10;
                // 第1个变随机数
                temp1 = (int) (Math.random() * 20 + 1);
                // 第2个变量赋值
                while (true) {
                        int temp = (int) (Math.random() * 20 + 1);
                        if (temp1 != temp) {
                                temp2 = temp;
                                break;
                        }
                }
                // 第3个随机数
                while (true) {
                        int temp = (int) (Math.random() * 20 + 1);
                        if (temp1 != temp && temp2 != temp) {
                                temp3 = temp;
                                break;
                        }
                }
                // 第4个随机数
                while (true) {
                        int temp = (int) (Math.random() * 20 + 1);
                        if (temp1 != temp && temp2 != temp && temp3 != temp) {
                                temp4 = temp;
                                break;
                        }
                }
                // 第5个随机数
                while (true) {
                        int temp = (int) (Math.random() * 20 + 1);
                        if (temp1 != temp && temp2 != temp && temp3 != temp
                                        && temp4 != temp) {
                                temp5 = temp;
                                break;
                        }
                }
                // 第6个随机数
                while (true) {
                        int temp = (int) (Math.random() * 20 + 1);
                        if (temp1 != temp && temp2 != temp && temp3 != temp
                                        && temp4 != temp && temp5 != temp) {
                                temp6 = temp;
                                break;
                        }
                }
                // 第7个随机数
                while (true) {
                        int temp = (int) (Math.random() * 20 + 1);
                        if (temp1 != temp && temp2 != temp && temp3 != temp
                                        && temp4 != temp && temp5 != temp && temp6 != temp) {
                                temp7 = temp;
                                break;
                        }
                }
                // 第8个随机数
                while (true) {
                        int temp = (int) (Math.random() * 20 + 1);
                        if (temp1 != temp && temp2 != temp && temp3 != temp
                                        && temp4 != temp && temp5 != temp && temp6 != temp
                                        && temp7 != temp) {
                                temp8 = temp;
                                break;
                        }
                }
                // 第9个随机数
                while (true) {
                        int temp = (int) (Math.random() * 20 + 1);
                        if (temp1 != temp && temp2 != temp && temp3 != temp
                                        && temp4 != temp && temp5 != temp && temp6 != temp
                                        && temp7 != temp && temp8 != temp) {
                                temp9 = temp;
                                break;
                        }
                }
                // 第10个随机数
                while (true) {
                        int temp = (int) (Math.random() * 20 + 1);
                        if (temp1 != temp && temp2 != temp && temp3 != temp
                                        && temp4 != temp && temp5 != temp && temp6 != temp
                                        && temp7 != temp && temp8 != temp && temp9 != temp) {
                                temp10 = temp;
                                break;
                        }
                }

                System.out.println(temp1);
                System.out.println(temp2);
                System.out.println(temp3);
                System.out.println(temp4);
                System.out.println(temp5);
                System.out.println(temp6);
                System.out.println(temp7);
                System.out.println(temp8);
                System.out.println(temp9);
                System.out.println(temp10);
        }
}
有木有一个简单的思路,不使用数组?

27 个回复

正序浏览
#在这里private static void getNum() {                 //创建一个HashSet集合,它的特点是在添加前会判断有没有一样的元素,有就不添加                 HashSet<Integer> hs= new HashSet<Integer>();                 //创建一个生成器,添加到集合里,当集合里的元素有十个跳出循环;                 while(hs.size() < 10) {                         int i = (int)(Math.random()*20+1);                         hs.add(i);                 }                 //打印                 System.out.println(hs);         }快速回复#
回复 使用道具 举报
沙发....................
回复 使用道具 举报
厉害厉害呀,呵呵,学习了
回复 使用道具 举报
这种题明显的要使用循环呢!
回复 使用道具 举报
路过学习一下````
回复 使用道具 举报
public static void main(String[] args) {
            ArrayList<Integer> list=new ArrayList<Integer>();
            Random rand=new Random();
            int num;
            while(list.size()<10){
                    num=rand.nextInt(20)+1;
                    if(!list.contains(num)){
                            list.add(num);
                    }
            }
            
            for(Integer i:list){
                    System.out.println(i);
            }
       
        }
回复 使用道具 举报
一楼的代码看的人都醉了
回复 使用道具 举报
Set集合不可以存储重复元素啊
回复 使用道具 举报
表示目前只会获取随机数,集合什么的还没开始学
回复 使用道具 举报
{:2_38:}我晕了
回复 使用道具 举报
用随机算法可以生成啊,很快的
回复 使用道具 举报
  1. import java.util.HashSet;
  2. import java.util.Iterator;
  3. import java.util.Set;


  4. public class Test01 {
  5.         public static void main(String[] args) {
  6.                  
  7.                  Set<Integer> a = new HashSet<Integer>();
  8.                  while(a.size()<10){
  9.                          int temp = (int) (Math.random() * 20 + 1);
  10.                          a.add(temp);
  11.                  }
  12.                  Iterator<Integer> iterator = a.iterator();
  13.                  while(iterator.hasNext()){
  14.                          System.out.println(iterator.next());
  15.                  }
  16.         }
  17. }
复制代码
回复 使用道具 举报
ganjx 中级黑马 2015-10-6 01:40:52
15#
先把求出来的存起来不行吗?
回复 使用道具 举报
思路:
        1、获取随机数,就用Random
        2、随机数不能重复,就用set集合
          3、长度为10,让set集合长度<=10
回复 使用道具 举报
好常见的题 这个
回复 使用道具 举报
很厉害的样子
回复 使用道具 举报
你写的不是随机的,每个数产生的概率不一样.可以使用随机算法生成.
回复 使用道具 举报
无序的随机方式应该可以直接调用方法实现
回复 使用道具 举报
        public static void main(String[] args) {
               
               
                for (Integer integer : getRandom(10, 20)) {
                        System.out.println(integer);
                }
               
        }
         private static HashSet<Integer> getRandom(int x,int y){
                 
                 //定义一个HashSet集合,用来存储随机数。利用Set集合中元素唯一性来使随机数不重复
                 HashSet<Integer> hs=new HashSet<Integer>();
                 
                 //使用for循环来获取随机数
                        for (int i = 0; hs.size()< x; i++) {
                               
                                //创建一个随机数生成器。
                                Random rd1=new Random();
                               
                                //调用Random类中的nextInt()方法来获取指定范围内的随机数。(0~y)
                                int num=rd1.nextInt(y)+1;
                                hs.add(num);
                        }
                        return hs;
            }
回复 使用道具 举报
文森 中级黑马 2015-5-10 10:41:55
8#
觉得用HashSet方便些
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马