Java 常用类库 之 Random 随机数类实例
Posted verejava
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 常用类库 之 Random 随机数类实例相关的知识,希望对你有一定的参考价值。
http://www.verejava.com/?id=169931132381103
/**
知识点:Random 随机数类
实际应用: 敌人飞机随机从屏幕的上方出现
*/
import java.util.*;
public class TestRandomCard
{
public static void main(String[] args)
{
String[] cards={"A","A","A","A",
"2","2","2","2",
"3","3","3","3",
"4","4","4","4",
"5","5","5","5",
"6","6","6","6",
"7","7","7","7",
"8","8","8","8",
"9","9","9","9",
"10","10","10","10",
"J","J","J","J",
"Q","Q","Q","Q",
"K","K","K","K",
"小王","大王"
};
System.out.println("原牌:");
for(int i=0;i<cards.length;i++)
{
System.out.print(cards[i]+",");
}
//随机打乱
Set set=new HashSet();
Random rn=new Random();
while(true)
{
set.add(rn.nextInt(54));
if(set.size()==54)
{
break;
}
}
System.out.println("
54个随机索引号:");
Object[] iter=set.toArray();
for(int i=0;i<iter.length;i++)
{
System.out.print(iter[i]+",");
}
List zhanshan=new ArrayList();
List lisi=new ArrayList();
List wangwu=new ArrayList();
}
}
http://www.verejava.com/?id=169931132381103
以上是关于Java 常用类库 之 Random 随机数类实例的主要内容,如果未能解决你的问题,请参考以下文章