每次用户输入相应的值时,通过添加卡片来创建一副牌

时间:2013-11-12 12:03:42

标签: java

我试图通过用户输入制作一副纸牌。例如。如果用户输入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];
        }






}

1 个答案:

答案 0 :(得分: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;
    }
}