随机产生13个0~51不同的随机数 -思想(定义参考系)

来源:互联网 发布:iphone6移动数据打不开 编辑:程序博客网 时间:2024/06/06 13:21
  1. /*
  2.  * 随机产生13个0~51没有重复的随机数
  3.  * class : arrayok
  4.  *
  5.  */
  6.  public class arrayok
  7.  {
  8.      public staticvoid main(String args[])
  9.      {
  10.          int suit[]= new int[13];//存储13个随机数

  11.          boolean sw[]= new boolean[52];//随机数存在。则为真,否则为假

  12.          int key= 0;
  13.          for(int i= 0; i < suit.length; i++)
  14.          {
  15.              while(true)
  16.              {
  17.                  key = (int)(Math.random()*52);
  18.                  if(sw[key]== false){
  19.                      break;
  20.                  }
  21.              }
  22.              suit[i]= key;
  23.              sw[key]= true;
  24.          }
  25.          for(int i= 0; i < suit.length; i++)
  26.          {
  27.              System.out.print(suit[i]+" ");
  28.          }
  29.          System.out.print("\n");
  30.      }
  31.  }
0 0
原创粉丝点击