用java撰写一个简单的洗牌程序,可在文本模式下显示洗牌结果

用java撰写一个简单的洗牌程序,可在文本模式下显示洗牌结果,第1张

//牌类

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())

}

}

}

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-1i>=1i--){

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=0i<cards.lengthi++){

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=0i<cards.lengthi++){

//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)

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11149485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存