主类:
公共课二十一点{
static Deck D;
static Hand P;
public static void main(String[] args) {
init();
}
private static void init() {
D=new Deck();
P=new Hand(D);
startGame();
}
private static void startGame() {
System.out.print("Your cards are: "+P.H.get(0)+", "+P.H.get(1));
}
}
手类:
import java.util.ArrayList;
import java.util.Random;
public class Hand {
ArrayList<String> H;
Random R;
public Hand(Deck D){
R=new Random();
H=new ArrayList<String>();
int C=R.nextInt(D.getP().length);
H.add(D.getP()[C]);
D.removeFromDeck(C);
int E=R.nextInt(D.getP().length);
H.add(D.getP()[E]);
D.removeFromDeck(E);
}
}
甲板等级
public class Deck {
String[] P;
public Deck(){
P=new String[52];
String Suit="Default";
int C=0;
for(int A=1;A<=4;A++){
switch(A){
case 1:
Suit="Hearts";
break;
case 2:
Suit="Diamonds";
break;
case 3:
Suit="Clubs";
break;
case 4:
Suit="Spades";
break;
}
for(int B=1;B<=13;B++){
if(B>10){
switch(B){
case 11: P[C]="Joker of "+Suit;
break;
case 12: P[C]="Queen of "+Suit;
break;
case 13: P[C]="King of "+Suit;
break;
}
}else{
P[C]=B+" of "+Suit;
}
}
}
}
public void setP(String[] p) {
P = p;
}
public String[] getP() {
return P;
}
public void removeFromDeck(int C){
System.arraycopy(P, C + 1, P, C,
P.length - C - 1);
}
}
当我编译并运行这段代码时,它打印出我的 2 张卡是空的,空的。我查看了代码,似乎找不到我的错误。也许你可以?TY 为您提供任何帮助。
编辑:它现在只返回黑桃,有人可以帮忙吗?