public class Play {
/**
* 玩牌
*/
public static void main(String[] args) {
Poker poker = new Poker()
boolean over = false
Scanner cin=new Scanner(System.in)
while(!over){
System.out.println("打牌输入1,展示剩余牌输入其他字符:")
String c=cin.nextLine()
if("1".equals(c)){
int index = (int) (Math.random()*poker.getSize())
poker.remove(index)
}else{
poker.ShowPages()
}
}
/*for(int i = 0i<54i++){
int index = (int) (Math.random()*poker.getSize())
poker.remove(index)
poker.ShowPages()
}
*/
}
}二://牌
public class Page { private String huase = ""
private String haoma = ""
public Page(String huase,String haoma){
this.huase = huase
this.haoma = haoma
}
public String show(){
return this.huase+this.haoma
}
}三:import java.util.ArrayList
import java.util.Iterator
import java.util.List
/**
* 一副扑克
*/
public class Poker {
private List pages = new ArrayList()
public Poker(){
String huase[] = new String[]{"黑桃","红桃","草花","方块"}
String haoma[] = new String[]{"A","2","3","4","5","6","7","8","9","10"
,"J","Q","K"}
for(int i = 0i<haoma.lengthi++){
for(int j = 0j<huase.lengthj++){
pages.add(new Page(huase[j],haoma[i]))
}
}
pages.add(new Page("","大王"))
pages.add(new Page("","小王"))
}
public void ShowPages(){
System.out.println("展示剩余牌:")
for(Iterator i = pages.iterator()i.hasNext()){
System.out.println(((Page)i.next()).show())
}
}
//打牌
public void remove(int index){
pages.remove(index)
}
//剩余牌数
public int getSize(){
return pages.size()
}
}
修改的部分我加了#########你看一下吧//这个初始化方法
publicvoidinitcard()
{
String num[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}
String suit[]={"方块","梅花","红桃","黑桃"}
String bigwang,xiaowang
card = newCardClass[54]//#######初始化54张而不是52张
for(inti=0i<52i++)
{
card[i] = newCardClass(num[i%13],suit[i/13])
}
car[52] = new CardClass("大王","")//#####初始化大王
car[53] = new CardClass("小王","")//######初始化小王
}
publicvoidshufflecard()//洗牌
{
Random rd = newRandom()
for(inti=0i<54i++) //##########由52变为54
{
int j = rd.nextInt(54)//生成随机数 #######52变54
CardClass temp = card[i]//交换
card[i]=card[j]
card[j]=temp
}
}
publicvoiddealcard()//发牌
{
for(inti=0i<54i++)//##########由52变成54
{
if(i%12==0) System.out.println("\n")
System.out.print(card[i])
}
}
使用JAVA编写以下程序:1、请定义一个名为Card的扑克牌类,该类有两个private访问权限的字符串变量face和suit;分别描述一张牌的牌面值(如:A、K、Q、J、10、9、……3、2等)和花色(如:“黑桃”、“红桃”、“梅花”、“方块”);定义Card类中的public访问权限的构造方法,为类中的变量赋值;定义protected访问权限的方法getFace(),得到扑克牌的牌面值;定义protected访问权限的方法getSuit(),得到扑克牌的花色;定义方法toString(),返回表示扑克牌的花色和牌面值字符串(如“红桃A”、“梅花10”等)。
2、若应用程序的main方法中,定义字符串数组f和s;分别表示扑克牌的牌面值和花色;定义52个元素的Card类型数组deck,用来存放4个花色的52张牌。如下所示。
String f[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}
String s[] = {"黑桃","红桃","梅花","方块"}
Card deck = new Card[52];
(1)使用Card类的构造方法给deck数组的52张牌赋值,要求数组中先存放黑桃花色的A、2、3、……、K;然后是红桃花色的A、2、3、……、K;梅花花色的A、2、3、……、K;方块花色的A、2、3、……、K。请写出实现上述功能的程序段。
(2)请编写模拟洗牌的完整程序,即把数组deck中的扑克牌随机打乱存放顺序。
原题如下:
引用内容
private String face// A, K, Q, J, 10, ... 2
private String suit// Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face
this.suit = suit
}
protected String getFace() {
return face
}
protected String getSuit() {
return suit
}
public String toString() {
return suit + " " + face
}
解题,完成程序解:
程序代码
package poker
public class Card {
private String face// A, K, Q, J, 10, ... 2
private String suit// Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face
this.suit = suit
}
protected String getFace() {
return face
}
protected String getSuit() {
return suit
}
public String toString() {
return suit + " " + face
}
public static void shuffle(Card[] deck, int startIndex, int size,
int splitIndex) { //shuffle洗牌
System.out.println("startIndex:" + startIndex)
if (splitIndex * 2 >size) { //if rand is 30
Card.swap(deck, startIndex, splitIndex, size - splitIndex)//[52]、0、30、22
shuffle(deck, size - splitIndex, splitIndex, size - splitIndex)//[52]、22、30、22
} else if (splitIndex * 2 <size) { //if rnd is 10
Card.swap(deck, startIndex, size - splitIndex, splitIndex)//[52]、0、42、10
shuffle(deck, startIndex, size - splitIndex, splitIndex)//[52]、0、42、10
} else {
Card.swap(deck, startIndex, splitIndex, splitIndex)
}
}
public static void swap(Card[] deck, int srcIndex, int dstIndex, int size) { //交换
//SRC是在本位置显示:source的缩写,源的意思 HREF是点击后连接的目标:HyperlinkReference,超链接引用
String face = ""
String suit = ""
for (int i = 0i <sizei++) {
face = deck[srcIndex + i].face
suit = deck[srcIndex + i].suit
deck[srcIndex + i].face = deck[dstIndex + i].face
deck[srcIndex + i].suit = deck[dstIndex + i].suit
deck[dstIndex + i].face = face
deck[dstIndex + i].suit = suit
}
}
/**
* @param args
*/
public static void main(String[] args) {
//第一问解答
Card[] deck = new Card[52]
String f[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K"}
String s[] = {"Spade", "Heart", "Club", "Diamond"}
for (int i = 0i <s.lengthi++) {
for (int j = 0j <f.lengthj++) {
deck[i * 13 + j] = new Card(s[i], f[j])//依次存入数组之中,每13张一回合
}
}
//第二问解答
int rnd = 0
int numOfShuffle = 10
for (int i = 0i <numOfShufflei++) {
rnd = (int) Math.abs(Math.random() * 52)//Math.random()返回大于或等于 0.0 但小于 1.0 的伪随机 double 值。
Card.shuffle(deck, 0, deck.length, rnd)
}
// Test
/*
for (int i=0i<deck.lengthi++)
{
System.out.println(deck[i])
} */
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)