189 8069 5689

java随机洗牌代码 java洗牌程序

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,请多指教,谢谢!!!

package xly04;

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撰写一个简单的洗牌程序,可在文本模式下显示洗牌结果

//牌类

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随机洗牌代码 java洗牌程序
地址分享:http://jkwzsj.com/article/hpppoc.html

其他资讯