jeudi 24 janvier 2019

return integer value of random string key

This is my BlackJack project and it currently only returns 1 card. I am struggling to get the randomKey to be equal to its value in the HashMap

public static void main(String[] args) {

    Random ran = new Random();
    Scanner in = new Scanner(System.in);

    HashMap<String, Integer> hash = new HashMap<String, Integer>();
    hash.put("2", 2);
    hash.put("3", 3);
    hash.put("4", 4);
    hash.put("5", 5);
    hash.put("6", 6);
    hash.put("7", 7);
    hash.put("8", 8);
    hash.put("9", 9);
    hash.put("10", 10);
    hash.put("Jack", 10);
    hash.put("Queen", 10);
    hash.put("King", 10);
    hash.put("Ace", 11);

    List<String> keyList = new ArrayList<>(hash.keySet());
    List<Integer> valueList = new ArrayList<>(hash.values());


    String[] colors = {"Spades", "Hearts", "Diamonds", "Clubs"};

    for (int i = 0; i < 1; i++) {
        int col = ran.nextInt(colors.length); // random color
        String randomKey = keyList.get(ran.nextInt(hash.size())); // random type
        int keyValue = valueList.get(ran.nextInt(hash.size())); 

        System.out.println(randomKey+ " of "+colors[col]);
        if (keyValue < 21) {System.out.println(keyValue);}
    } // returns a cards

I used the if (keyValue < 21) {System.out.println(keyValue);} to test if the keyValue would be equal to the unique value of the random key. Obviously it isnt, but I need it to be so I can then calculate the sum of multiple cards, while printing out only the keys.




Aucun commentaire:

Enregistrer un commentaire