您的位置主页 > 综合专区 > 个人习作 > 生成5个随机数,且不重复

生成5个随机数,且不重复

2009-07-14    文章来源:互联网    浏览次数:1440     分享文章
 
  1. import java.util.Random;
  2. class Randoms
  3. {
  4.     public static void main(String args[]){
  5.         int[] randoms = new int[]{1,2,3,4,5};
  6.         Random rnd = new Random();
  7.         int x = 0;
  8.         int count = 0;
  9.         while(count != 5)
  10.         {
  11.              x = rnd.nextInt(5);
  12.              if(randoms[x] != -1)
  13.              {
  14.                  System.out.println(randoms[x]);
  15.                  randoms[x] = -1;
  16.                  count ++;
  17.              }
  18.         } 
  19.     }
  20. }

 

文章评论(查看全部)

昵 称 *
电子邮箱 *
网 址      8 + 4 = ?