Java 创建一副牌并洗牌
我已经在这上面停留了一段时间了,目标是创建一个阵列来容纳卡片,一旦我创建了阵列,打印出来,唯一正在打印的卡片就是心之王。为什么它不是迭代的Java 创建一副牌并洗牌,java,Java,我已经在这上面停留了一段时间了,目标是创建一个阵列来容纳卡片,一旦我创建了阵列,打印出来,唯一正在打印的卡片就是心之王。为什么它不是迭代的 public class Card { // Card suits (provided for your convenience - use is optional) public static final int SPADES = 0; public static final int HEARTS = 1; publ
public class Card
{
// Card suits (provided for your convenience - use is optional)
public static final int SPADES = 0;
public static final int HEARTS = 1;
public static final int CLUBS = 2;
public static final int DIAMONDS = 3;
// Card faces (provided for your convenience - use is optional)
public static final int ACE = 1;
public static final int TWO = 2;
public static final int THREE = 3;
public static final int FOUR = 4;
public static final int FIVE = 5;
public static final int SIX = 6;
public static final int SEVEN = 7;
public static final int EIGHT = 8;
public static final int NINE = 9;
public static final int TEN = 10;
public static final int JACK = 11;
public static final int QUEEN = 12;
public static final int KING = 13;
// define fields here
private static int suit;
private static int val;
private String[] suits = {"Clubs", "Spades", "Diamonds", "Hearts"};
private String[] vals = {"Ace","2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King" };
// This constructor builds a card with the given suit and face, turned face down.
public Card(int suit, int val)
{
this.val = val;
this.suit = suit;
}
public @Override String toString()
{
return vals[val] + " Of " + suits[suit];
}
// This method retrieves the suit (spades, hearts, etc.) of this card.
public int getSuit()
{
return this.suit;
}
// This method retrieves the face (ace through king) of this card.
public int getFace()
{
// return this.val;
switch(val)
{
case 0 : return 1;
case 1 : return 2;
case 2 : return 3;
case 3 : return 4;
case 5 : return 6;
case 6 : return 7;
case 7 : return 8;
case 8 : return 9;
case 9 : return 10;
case 10 : return 10;
case 12 : return 10;
default : return 0;
}
}
}`
import java.util.ArrayList;
import java.util.Random;
// This class represents the deck of cards from which cards are dealt to players.
public class Deck
{
//array
public static Card[] cards;
// private static ArrayList<Card> cards;
int i;
int counter;
// This constructor builds a deck of 52 cards.
Deck()
{
i = 51;
//array implementation
cards = new Card[52];
int x = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 13;j++)
{
//Array implementation
cards[x] = new Card(i,j);
x++;
}
}
}
// This method takes the top card off the deck and returns it.
public Card deal()
{
//Array implementation
int index = 0;
Card temp = cards[index];
return temp;
}
// this method returns true if there are no more cards to deal, false otherwise
public boolean isEmpty()
{
if(cards.length == 0)
{
return true;
}
return false;
}
//this method puts the deck int some random order
// public void shuffle()
// {
// for(int i = 51; i > 0; i--)
// {
// int rand = (int) (Math.random() * (i+1));
// Card temp = this.cards[i];
// this.cards[i] = cards[rand];
// cards[rand] = temp;
// }
//
// }
}
public class BlackJack extends Deck
{
public static void main(String []args)
{
System.out.println("Ready to play a game of BlackJack?");
Deck deck = new Deck();
for(int i = 0; i < cards.length;i++)
{
System.out.println(cards[i]);
}
公共类卡
{
//卡片套装(为方便您而提供-使用是可选的)
公共静态最终整数黑桃=0;
公共静态最终整数=1;
公共静态最终积分=2;
公共静态最终整数钻石=3;
//卡片面(为方便您而提供-使用是可选的)
公共静态最终int ACE=1;
公共静态最终int 2=2;
公共静态最终int 3=3;
公共静态最终int 4=4;
公共静态最终int 5=5;
公共静态最终int 6=6;
公共静态最终整数7=7;
公共静态最终整数8=8;
公共静态最终int 9=9;
公共静态最终整数=10;
公共静态最终int插孔=11;
公共静态最终整数=12;
公共静态最终int KING=13;
//在此定义字段
私服;
私有静态int-val;
私人字符串[]套装={“梅花”、“黑桃”、“钻石”、“红心”};
私有字符串[]VAL={“Ace”、“2”、“3”、“4”、“5”、“6”、“7”,
“8”、“9”、“10”、“杰克”、“女王”、“国王”};
//该构造器构建一张具有给定套装和正面的卡片,正面朝下。
公共卡(内部诉讼,内部val)
{
this.val=val;
这套衣服;
}
public@Override String toString()
{
返回“+诉讼[诉讼]的val[val]+”;
}
//此方法检索此卡的套装(黑桃、红桃等)。
公共int getSuit()
{
退回这件衣服;
}
//此方法检索此卡的面(ace到king)。
公共int getFace()
{
//返回此.val;
开关(val)
{
案例0:返回1;
案例1:返回2;
案例2:返回3;
案例3:返回4;
案例5:返回6;
案例6:返回7;
案例7:返回8;
案例8:返回9;
案例9:返回10;
案例10:返回10;
案例12:返回10;
默认值:返回0;
}
}
}`
导入java.util.ArrayList;
导入java.util.Random;
//这个类别代表一副牌,从中发牌给玩家。
公务舱甲板
{
//排列
公共静态卡[]卡;
//私有静态数组列表卡;
int i;
整数计数器;
//这个构造器构建了一副52张卡片。
甲板()
{
i=51;
//数组实现
卡片=新卡片[52];
int x=0;
对于(int i=0;i<4;i++)
{
对于(int j=0;j<13;j++)
{
//数组实现
卡片[x]=新卡片(i,j);
x++;
}
}
}
//此方法从牌组中取出最上面的牌并将其返回。
公共卡交易()
{
//数组实现
int指数=0;
卡片温度=卡片[索引];
返回温度;
}
//如果没有更多的牌可供交易,此方法返回true,否则返回false
公共布尔值为空()
{
if(cards.length==0)
{
返回true;
}
返回false;
}
//此方法将甲板置于某种随机顺序
//公开无效洗牌()
// {
//对于(int i=51;i>0;i--)
// {
//int rand=(int)(Math.random()*(i+1));
//卡片温度=此卡片[i];
//这个.cards[i]=cards[rand];
//卡片[兰特]=临时工;
// }
//
// }
}
公营21点扩展甲板
{
公共静态void main(字符串[]args)
{
System.out.println(“准备好玩21点游戏了吗?”);
甲板=新甲板();
对于(int i=0;i
问题由以下原因引起:
// define fields here
private static int suit;
private static int val;
因为它们是静态的,所以所有卡实例都共享这些值,因此它们都将具有设置为的最后一个值。请删除静态值。问题是由以下原因造成的:
// define fields here
private static int suit;
private static int val;
因为它们是静态的,所以所有卡实例都共享这些值,因此它们都将具有设置为的最后一个值。请删除静态值。为您提供一些提示,说明如何使代码更易于阅读/维护:
- 使用枚举而不是常量。你可以像类一样给它们构造函数/成员变量
- 对于每个循环使用,它更易于阅读,并防止了常见的1对1问题
- 在所有其他修饰符之前键入“static”,并将所有静态变量和成员与任何非静态内容分开。这样,您就永远不会意外地混淆它们
- 我使用静态嵌套的枚举和类。通常每个枚举和类都应该放在自己的文件中,并且是非静态的
- 我喜欢根据变量的作用域给它们加前缀,这样就不会弄乱它们:pXxx表示参数,sXxx表示静态变量,mxxx表示成员,没有前缀表示方法变量
静态公共枚举类型{
黑桃、红桃、梅花、钻石
}
静态公共枚举FaceType{
ACE(“ACE”,1)//
两个(“2”,2)//
三个(“3”,3)//
四个(“4”,4)//
五个(“5”,5)//
六个(“6”,6)//
七(“7”,7)//
八个(“8”,8)//
九(“9”,9)//
十(“10”,10)//
杰克(“杰克”,11岁)//
女王(“女王”,12岁)//
国王(“国王”,13岁)//
;
私有最终字符串mName;
私人最终价值;
专用FaceType(最终字符串pName,最终int pValue){
mName=pName;
mValue=pValue;
}
公共Stri