【问题标题】:Creating a deck of cards by adding a card each time a user inputs a corresponding value [duplicate]每次用户输入相应的值时,通过添加一张卡片来创建一副卡片[重复]
【发布时间】:2013-11-12 12:03:41
【问题描述】:

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






}

【问题讨论】:

  • 你为什么要再次发布相同(或非常相似)的问题?
  • 这段代码至少不会编译。
  • 您认为再次发布此问题会比一小时前获得更高的回答机会吗?

标签: java


【解决方案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-24
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 2021-01-31
    • 1970-01-01
    • 2021-07-21
    • 2018-05-12
    • 2021-07-07
    相关资源
    最近更新 更多