java代码随机洗牌 java生成随机数
请问用java数组编写洗牌程序怎么写?我刚学java,请多指教,谢谢!!!
package xly04;
创新互联建站是一家专注网站建设、网络营销策划、小程序设计、电子商务建设、网络推广、移动互联开发、研究、服务为一体的技术型公司。公司成立十年以来,已经为上1000+成都iso认证各业的企业公司提供互联网服务。现在,服务的上1000+客户与我们一路同行,见证我们的成长;未来,我们一起分享成功的喜悦。
import java.util.Random;
public class ArrayDemo3 {
/**
* 数组应用:
* 1 随机洗牌
* 2 顺序发牌
*/
public static void main(String[] args) {
String[] cards=new String[]{"黑桃A","黑桃2","黑桃3","黑桃4",
"黑桃5","黑桃6","黑桃7","黑桃8","黑桃9","黑桃10","黑桃J","黑桃Q","黑桃K",
"红桃A","红桃2","红桃3","红桃4","红桃5","红桃6","红桃7","红桃8","红桃9",
"红桃10","红桃J","红桃Q","红桃K","方块A","方块2","方块3","方块4","方块5",
"方块6","方块7","方块8","方块9","方块10","方块J","方块Q","方块K","梅花A",
"梅花2","梅花3","梅花4","梅花5","梅花6","梅花7","梅花8","梅花9","梅花10",
"梅花J","梅花Q","梅花K","大王","小王"};
//Random random=new Random();.......(1)
//int index=random.nextInt(10);//[0,10) %10
//System.out.println(index);
//String card=cards[random.nextInt(cards.length)];//长度为8,随机下标[0,8)
//System.out.println(card);//随机的某张牌
for(int i=cards.length-1;i=1;i--){
int j=new Random().nextInt(i);//或者(1)不注销用int j=random.nextInt(i);
String t=cards[i];
cards[i]=cards[j];
cards[j]=t;
}
for(int i=0;icards.length;i++){
System.out.print(cards[i]+",");
}
System.out.println();//输出回车字符
//顺序发牌:顺序的将每个牌发给每个人
String[] players=new String[]{"马伊琍","文章","六六"};
//int i=0;
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
for(int i=0;icards.length;i++){
//i=0,1,2,3,4,5,6,7
String card=cards[i];//card 代表每张扑克牌
String player=players[i%players.length];
//player代表每个玩游戏的人
System.out.println(player+":"+card);
}
}
}
用JAVA写个代码,实现52张牌随机分给四个人
java内的Collections类自带了一个shuffle洗牌算法。
static void shuffle(List? list)
使用默认随机源对指定列表进行置换。
static void shuffle(List? list, Random rnd)
使用指定的随机源对指定列表进行置换。
现在你可以把52张牌放进一个List里,调用他的shuffle算法打乱顺序。
用java撰写一个简单的洗牌程序,可在文本模式下显示洗牌结果
//牌类
public class Card {
private int face; //牌的面值
private int suit; //牌的花色
public Card(int face, int suit) {
super();
this.face = face;
this.suit = suit;
}
public int getFace() {
return face;
}
public void setFace(int face) {
this.face = face;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
}
//模拟洗牌和发牌
public class Test {
public static void main(String[] args) {
Card[] deck = new Card[52];
fillDeck(deck);
deal(deck);
System.out.println();
}
private static void fillDeck(Card[] deck) {
int i, j=0;
for (i = 0; i 52; i++) {
j = (int) (Math.random() * 52);
deck[i]=new Card(j%13,j/13);
}
}
private static void deal(Card[] deck) {
int k1, k2;
System.out.println();
for (k1 = 0, k2 = k1 + 26; k1 26; k1++, k2++) {
if (k1 % 13 == 0)
System.out.println("\n第" + (k1 / 13 + 1) +
"组牌:\t\t\t第"+ (k1 / 13 + 3) + "组牌:");
System.out.print("\nCard: " + deck[k1].getFace() +
" Suit: "+ deck[k1].getSuit());
System.out.print("\t\t\tCard: " + deck[k2].getFace() +
" Suit: "+ deck[k2].getSuit());
}
}
}
java洗牌算法问题
你指的是Card里的toString方法吧
public String toString() {
String aa = suit + " " + num + " ";
return aa;
}
toString这个方法一般在 System.out.print时使用,这个是打印出String,JVM就是默认调用类的toSting方法
注:所有类都有toString方法,默认是当前对象的hashcode,即内存地址
所以在发牌是打印
public void dealcard()//发牌
{
for(int i=0;i52;i++)
{
if(i%4==0i!=0){
System.out.println(); //每发4张牌输出换行
}
// 就是这里,默认调用card的toString方法
System.out.print(card[i]); //依次输出 发的牌
}
}
分享名称:java代码随机洗牌 java生成随机数
当前URL:http://scyanting.com/article/dospsco.html