How to operate the poker game by Java?

前端 未结 4 1812
Happy的楠姐
Happy的楠姐 2021-01-16 17:44

I’ve learned Java for 1 month. This time, I’d like to create a poker game. There are two questions about my program. I hope somebody can help me to fix it.

4条回答
  •  不思量自难忘°
    2021-01-16 18:46

    For your first question, you could map a card name to its value:

    Map values = new HashMap();
    values.put("A", 11);
    values.put("2", 2);
    // ...
    values.put("King", 10);
    
    // lookup a value:
    int kingValue = values.get("King");
    

    For the second, I'd rely on java shuffle function. Create a list of all cards

    ArrayList deck = someMagicMethodToPopulateTheList();
    

    and then simply shuffle the deck

    Collections.shuffle(deck);
    

    Now you can iterate through the deck and you will see the cards in a random order:

    for (Card card:deck) {
      System.out.println(card);
    }
    

    Note: the Card class does not extist (yet). I simply invented the name to keep it simple)

提交回复
热议问题