0
我正在製作一個涉及一副撲克牌的程序。我知道有很多方法可以製作一副撲克牌,但在這種情況下,我會將每張牌作爲對象。該對象存儲6個值:顏色,花色,面值,值和IDNumber。對象數組 - 數據沒有正確存儲在一副撲克牌上
然後通過一種名爲DeckOfCardsInitialiser的方法,將52個卡片對象存儲在一個數組中。該方法初始化卡,即它計算和設置對象(即卡)變量。
當檢查卡片設置是否正確時,我發現所有52張卡片都分配給僅用於最後一張卡片的值,在這種情況下爲黑桃王。我已經做了一些調試,並知道DeckOfCardsInitialiser按照設計工作。它只是數組的對象,並不是按計劃運行。
這是我第一次使用對象數組,這讓我思考我是否犯了一些愚蠢的錯誤。該程序從Test - > DeckOfCardsInitialiser - > Card運行。我將以相反的順序打印,以便更易於遵循。
卡
public class Card{
private static String suit ="";
private static String face="";
private static String colour="";
private static int value;
private static int rank;
private static int idNum;
public Card(){
}
public static void setSuit(String x){
suit = x;
}
public static void setFace(String x){
face = x;
}
public static void setColour(String x){
colour = x;
}
public static void setValue(int x){
value = x;
}
public static void setRank(int x){
rank = x;
}
public static void setIdNum(int x){
idNum = x;
}
public static String getSuit(){
return suit;
}
public static String getFace(){
return face;
}
public static String getColour(){
return colour;
}
public static int getValue(){
return value;
}
public static int getRank(){
return rank;
}
public static int getIdNum(){
return idNum;
}
}
DeckofCardsInitialiser
public class DeckOfCardsInitialiser{
private static Card [] deckOfCards = new Card[52]; // makes an array with 52 cards
// ================================================== Constructor
// Constructor runs through all 52 'cards' and sets values to each one
public DeckOfCardsInitialiser(){
for (int counter = 0; counter < deckOfCards.length; counter++){
deckOfCards[counter] = new Card();
setSuits(counter);
setValues(counter);
}
}
// Sets suit type and colour to each card
public static void setSuits(int x){
if(x >= -1 && x <= 11){
deckOfCards[x].setSuit("Hearts");
deckOfCards[x].setColour("Red");
}
if(x >= 12 && x <= 24){
deckOfCards[x].setSuit("Spades");
deckOfCards[x].setColour("Black");
}
if(x >= 25 && x <= 37){
deckOfCards[x].setSuit("Diamonds");
deckOfCards[x].setColour("Red");
}
if(x >= 38 && x <= 51){
deckOfCards[x].setSuit("Spades");
deckOfCards[x].setColour("Black");
}
}
// Sets face, rank, value & IDnum for each card
public static void setValues(int x){
if (x == 0 || x == 13 || x == 26 || x == 39){
deckOfCards[x].setFace("Ace");
deckOfCards[x].setRank(13);
deckOfCards[x].setValue(1);
deckOfCards[x].setIdNum(x);
}
if (x == 1 || x == 14 || x == 27 || x == 40){
deckOfCards[x].setFace("Two");
deckOfCards[x].setRank(1);
deckOfCards[x].setValue(2);
deckOfCards[x].setIdNum(x);
}
if (x == 2 || x == 15 || x == 28 || x == 41){
deckOfCards[x].setFace("Three");
deckOfCards[x].setRank(2);
deckOfCards[x].setValue(3);
deckOfCards[x].setIdNum(x);
}
if (x == 3 || x == 16 || x == 29 || x == 42){
deckOfCards[x].setFace("Four");
deckOfCards[x].setRank(3);
deckOfCards[x].setValue(4);
deckOfCards[x].setIdNum(x);
}
if (x == 4 || x == 17 || x == 30 || x == 43){
deckOfCards[x].setFace("Five");
deckOfCards[x].setRank(4);
deckOfCards[x].setValue(5);
deckOfCards[x].setIdNum(x);
}
if (x == 5 || x == 18 || x == 31 || x == 44){
deckOfCards[x].setFace("Six");
deckOfCards[x].setRank(5);
deckOfCards[x].setValue(6);
deckOfCards[x].setIdNum(x);
}
if (x == 6 || x == 19 || x == 32 || x == 45){
deckOfCards[x].setFace("Seven");
deckOfCards[x].setRank(6);
deckOfCards[x].setValue(7);
deckOfCards[x].setIdNum(x);
}
if (x == 7 || x == 20 || x == 33 || x == 46){
deckOfCards[x].setFace("Eight");
deckOfCards[x].setRank(7);
deckOfCards[x].setValue(8);
deckOfCards[x].setIdNum(x);
}
if (x == 8 || x == 21 || x == 34 || x == 47){
deckOfCards[x].setFace("Nine");
deckOfCards[x].setRank(8);
deckOfCards[x].setValue(9);
deckOfCards[x].setIdNum(x);
}
if (x == 9 || x == 22 || x == 35 || x == 48){
deckOfCards[x].setFace("Ten");
deckOfCards[x].setRank(9);
deckOfCards[x].setValue(10);
deckOfCards[x].setIdNum(x);
}
if (x == 10 || x == 23 || x == 36 || x == 49){
deckOfCards[x].setFace("Jack");
deckOfCards[x].setRank(10);
deckOfCards[x].setValue(10);
deckOfCards[x].setIdNum(x);
}
if (x == 11 || x == 24 || x == 37 || x == 50){
deckOfCards[x].setFace("Queen");
deckOfCards[x].setRank(11);
deckOfCards[x].setValue(10);
deckOfCards[x].setIdNum(x);
}
if (x == 12 || x == 25 || x == 38 || x == 51){
deckOfCards[x].setFace("King");
deckOfCards[x].setRank(12);
deckOfCards[x].setValue(10);
deckOfCards[x].setIdNum(x);
}
public static String getSuit(int x){
String suit = deckOfCards[x].getSuit();
return suit;
}
public static String getFace(int x){
String face = deckOfCards[x].getFace();
return face;
}
public static String getColour(int x){
String colour = deckOfCards[x].getColour();
return colour;
}
public static int getValue(int x){
int value = deckOfCards[x].getValue();
return value;
}
public static int getRank(int x){
int rank = deckOfCards[x].getRank();
return rank;
}
public static int getIdNum(int x){
int idNum = deckOfCards[x].getIdNum();
return idNum;
}
}
}
測試
公共類的測試{
public static void main(String [] args){
DeckOfCardsInitialiser deckOfCards = new DeckOfCardsInitialiser();
for (int counter = 0; counter < 52; counter++){
System.out.printf("%s of %s\n",deckOfCards.getFace(counter), deckOfCards.getSuit(counter));
}
}
}
我非常感謝你托馬斯 - 這在過去的24小時裏一直讓我感到非常悲傷! – 2014-10-27 14:03:40
完成 - 工作 - 高興!再次感謝你 – 2014-10-27 14:16:03