我一次又一次地尝试过但是循环却被卡在了俱乐部之王身上。 目前我正在尝试生成一副牌并将其打印出来以检查牌组是否实际创建。
import java.util.*;
import java.math.*;
public class Deck extends Card{
private static Card[][]cardDeck=new Card[4][13];
int counter;
public Deck (String suit, String cardType, int value)
{
super(suit,cardType,value);
}
public void removeCard()
{
if(counter>=40)
generateDeck();
int Randr=(int)(Math.random()*26);
int Randc=(int)(Math.random()*2);
if(cardDeck[Randr][Randc]==null)
{
removeCard();
}
else
{
cardDeck[Randr][Randc]=null;
counter++;
}
}
public void print2DArray()
{
for(int i=0;i<cardDeck.length;i++)
{
for(int j=0;j<cardDeck[0].length;j++)
{
System.out.print(super.draw(cardDeck[i][j])+" "+j);
}
System.out.println();
}
}
public static void generateDeck()
{
for(int i=0; i<cardDeck.length;i++) {
for(int j=0;j<cardDeck[0].length;j++) {
if(i==0)
{
if(j==0)
{
cardDeck[0][0]=new Card("Clubs","Ace",1);
continue;
}
else if(j>0&&j<10)
{
cardDeck[i][j]=new Card("Clubs",""+(j+1),j+1);
continue;
}
else if(j==10)
{
cardDeck[i][j]=new Card("Clubs","Jack",10);
continue;
}
else if(j==11)
{
cardDeck[i][j]=new Card("Clubs","Queen",10);
continue;
}
else if(j==12)
{
cardDeck[i][j]=new Card("Clubs","King",10);
continue;
}
else if(i==1)
{
if(j==0)
{
cardDeck[1][0]=new Card("Hearts","Ace",1);
continue;
}
else if(j>0&&j<10)
{
cardDeck[i][j]=new Card("Hearts",""+(j+1),j+1);
continue;
}
else if(j==10)
{
cardDeck[i][j]=new Card("Hearts","Jack",10);
continue;
}
else if(j==11)
{
cardDeck[i][j]=new Card("Hearts","Queen",10);
continue;
}
else if(j==12)
{
cardDeck[i][j]=new Card("Hearts","King",10);
continue;
}
}
else if(i==2)
{
if(j==0)
{
cardDeck[2][0]=new Card("Spades","Ace",1);
continue;
}
else if(j>0&&j<10)
{
cardDeck[i][j]=new Card("Spades",""+(j+1),j+1);
continue;
}
else if(j==10)
{
cardDeck[i][j]=new Card("Spades","Jack",10);
continue;
}
else if(j==11)
{
cardDeck[i][j]=new Card("Spades","Queen",10);
continue;
}
else if(j==12)
{
cardDeck[i][j]=new Card("Spades","King",10);
continue;
}
}
else if(i==3)
{
if(j==0)
{
cardDeck[3][0]=new Card("Diamonds","Ace",1);
continue;
}
else if(j>0&&j<10)
{
cardDeck[i][j]=new Card("Diamonds",""+(j+1),j+1);
continue;
}
else if(j==10)
{
cardDeck[i][j]=new Card("Diamonds","Jack",10);
continue;
}
else if(j==11)
{
cardDeck[i][j]=new Card("Diamonds","Queen",10);
continue;
}
else if(j==12)
{
cardDeck[i][j]=new Card("Diamonds","King",10);
continue;
}
}
}
}
}
}
}
我的卡片类只是一个带有一些get方法的基本类
public class Card {
private static String suit;
private static int numVal;
private static String cardType;
public Card(String s,String cT, int val) {
suit=s;
numVal=val;
cardType=cT;
}
public String draw(Card card) {
return "Your card is "+card.getSuit()+" "+card.getCardType()+" its value is "+card.getValue();
}
public static String getSuit()
{
return suit;
}
public static String getCardType()
{
return cardType;
}
public static int getValue()
{
return numVal;
}
}
我的主要方法是检查它是否会打印
public class TestEnvironment {
public static void main(String[] args) {
Deck x= new Deck("","",0);
x.generateDeck();
x.print2DArray();
}
}
下面是我上面提到的if else语句的更新代码
import java.util.*;
import java.math。*; 公共课甲板扩展卡{
private static Card[][]cardDeck=new Card[4][13];
int counter;
public Deck (String suit, String cardType, int value)
{
super(suit,cardType,value);
}
public void removeCard()
{
if(counter>=40)
generateDeck();
int Randr=(int)(Math.random()*26);
int Randc=(int)(Math.random()*2);
if(cardDeck[Randr][Randc]==null)
{
removeCard();
}
else
{
cardDeck[Randr][Randc]=null;
counter++;
}
}
public void print2DArray()
{
for(int i=0;i<4;i++)
{
for(int j=0;j<cardDeck[0].length;j++)
{
System.out.print(i);
//System.out.println(super.draw(cardDeck[i][j])+" "+i+","+j);
}
System.out.println();
}
}
public static void generateDeck()
{ int value=0;
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
String[] ranks = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
for (int i=0;i<suits.length;i++)
for (int j=0;j<ranks.length;j++)
{
cardDeck[i][j] = new Card(suits[i],ranks[j],value);
if(j>9)
value =10;
else
value=j+1;
cardDeck[i][j] = new Card(suits[i],ranks[j],value);
}
答案 0 :(得分:0)
Collections.shuffle()
来重新排列ArrayList中的卡片。编辑:
但是这个循环停留在俱乐部之王。
你的generateCards()
方法使用了太多if / else语句来生成卡片。很难注意到哪个if / else语句可能导致问题。
您可以使用数组和循环来简化代码。
类似的东西:
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
String[] ranks = {"Ace", "2", "3", ... , "Jack", "Queen", "King"};
for (each suit) // assume i is the index
for (each rank) // assume j is the index
{
int value = (j < 10) ? 10 : j;
cardDeck[i][j] = new Card(...);
}
}
编辑2:
它被困在国王的黑桃
我猜你的问题如下:
private static String suit;
private static int numVal;
private static String cardType;
不要使用静态变量。 Static关键字表示Card类的所有实例将共享相同的值。