0

我正在尝试通过用户输入制作一副纸牌。例如,如果用户输入 0,4,则显示的卡片将存储为红桃 4。到目前为止,我的代码根本不适用于西装。如果卡的价值也一样,我也计划这样做。我在 card.nextLine()、int card[] 和 String suit 下遇到错误。有谁知道更简单的方法来做到这一点?

public void addCard() {

    String suit[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
    String value[] = {"ZZZZ", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

    System.out.println("Please enter a suit");
    Scanner input = new Scanner(System.in);
    int card[] = card.nextLine();

    int i;


        if(int card[] = 0){ 
            String newSuit [] = String suit[0];
        }

        else if(int card[] = 1){
            String newSuit [] = String suit[1];
        }

        else if (int card[] = 2){
            String newSuit [] = String suit [2];
        }

        else if (int card[] = 3){
            String newSuit [] = String suit [3];
        }






}
4

1 回答 1

1

也许你想要这样的东西

String suit[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
String value[] = {"ZZZZ", "Ace", "2", "3", "4", "5", "6", 
                  "7", "8", "9", "10", "Jack", "Queen", "King"};


String[] card = new String[5];  // only 5 cards are allowed here. Just example
String[] newSuit = String[5];

Scanner input = new Scanner(System.in);

for (int i = 0; i < 5; i++){
    System.out.println("Please enter a suit");
    int inputSuit = input.nextInt();
    System.out.println("Please enter a card");
    int inputValue = input.nextInt();

    switch (inputSuit) {
        case 0: newSuit[i] = suit[0]; break;
        case 1: newSuit[i] = suit[1]; break;
        case 2: newSuit[i] = suit[2]; break;
        case 3: newSuit[i] = suit[3]; break;
    }

    switch (inputValue) {
        case 1: card[i] = value[1]; break;
        case 2: card[i] = value[2]; break;
        case 3: card[i] = value[3]; break;
        case 4: card[i] = value[4]; break;
        case 5: card[i] = value[5]; break;
        case 6: card[i] = value[6]; break;
        case 7: card[i] = value[7]; break;
        case 8: card[i] = value[8]; break;
        case 9: card[i] = value[9]; break;
        case 10: card[i] = value[10]; break;
        case 11: card[i] = value[11]; break;
        case 12: card[i] = value[12]; break;
        case 13: card[i] = value[13]; break;
    }
}
于 2013-11-12T12:26:52.573 回答