生成5个随机数,且不重复
2009-07-14 文章来源:互联网 浏览次数:1440
分享文章
- import java.util.Random;
- class Randoms
- {
- public static void main(String args[]){
- int[] randoms = new int[]{1,2,3,4,5};
- Random rnd = new Random();
- int x = 0;
- int count = 0;
- while(count != 5)
- {
- x = rnd.nextInt(5);
- if(randoms[x] != -1)
- {
- System.out.println(randoms[x]);
- randoms[x] = -1;
- count ++;
- }
- }
- }
- }

文章评论(查看全部)