Java中的纸牌游戏问题

时间:2016-12-31 03:38:07

标签: java

在我的代码中,我找不到将方法human中的dealerPlayGame()变量分配到由DealCards()生成的各自字符串的方法。

每当我运行javac时,我都会收到错误String[] cannot be converted to String.但我不理解这一点,因为它们是方法dealerOtherDealCards()数组中的单独字符串。有人可以就这个问题给我一些帮助。

import java.util.Random;

public class GAME_8315085 {
static Random rand = new Random();

public static String[] MakeDeck() {
    String[] deck=new String[51];
    int k=0;
    String[] suits={"\u2660", "\u2661", "\u2662", "\u2663"};
    String[] ranks={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    for (int i=0;i<suits.length;i++) {
        for (int j=0;j<ranks.length;j++) {
            if (suits[i].equals("\u2663") && ranks[j].equals("Q")) {
                ;
            }
            else {
                deck[k]=ranks[j]+suits[i];
                k++;
            }
        }
    }
    return deck;
}

public static void ShuffleDeck(String[] deck) {
    for (int i=0;i<deck.length;i++) {
        int random = rand.nextInt(deck.length);
        String tmp=deck[i];
        deck[i] = deck[random];
        deck[random]=tmp;
    }
}

public static String[][] DealCards(String[] deck) {
    String[][] dealerOther = new String[2][26];
    int j=0;
    for (int i=0; i<=deck.length;i+=2) {
        dealerOther[0][j]=deck[i];
        j++;
    }
    j=0;
    for (int i=1; i<deck.length;i+=2) {
        dealerOther[1][j]=deck[i];
        j++;
    }
    return dealerOther;
}

public static void PlayGame() {
    String[] deck = MakeDeck();
    ShuffleDeck(deck);
    String[][] tmp=DealCards(deck);
    String human=tmp[0];
    String dealer=tmp[1];
}

public static void main(String[] args) {
    PlayGame();
}

}

1 个答案:

答案 0 :(得分:2)

问题在这里

String[][] tmp=DealCards(deck);
String human=tmp[0];
String dealer=tmp[1];

由于tmp是字符串数组(一种二维数组),humandealer必须是字符串数组。

将其更改为

String[][] tmp=DealCards(deck); 
String[] human=tmp[0];
String[] dealer=tmp[1];