jeudi 27 février 2020

If-else statement not evaluating to true when the condition should be true

I am attempting to create a program that draws 5 random cards from a deck of cards and stores the suit and value in 2 parallel arrays, then I have to check if there are duplicate cards and re-draw them if they are duplicate. To do this I have written the following code:

        for (int i = 0; i < suit.length; i++) {
            for (int j = 0; j < 5; j++) {
                randSuit = rand.nextInt(notSuit.length);
                randValue = rand.nextInt(notValue.length);

                suit[i] = notSuit[randSuit];
                value[i] = notValue[randValue];

                firstCards[i] = copyValue[randValue] + " of " + copySuit[randSuit];
            }        
    }

    for (int i = 0; i < suit.length; i++) {
        for (int j = 1; j < 5; j++) {
            if ((suit[i] == suit[j]) && (value[i] == value[j])) { // this if statement will not evaluate to true even when the conditions are true
                randSuit = rand.nextInt(notSuit.length);
                randValue = rand.nextInt(notValue.length);

                suit[i] = notSuit[randSuit];
                value[i] = notValue[randValue];

                verifiedCards[i] = copyValue[randValue] + " of " + copySuit[randSuit];
            }
        }        
    }

The bottom 2 for loops is meant to see if the card is duplicate and if so, redraw it and store that card in the verifiedCards array. Instead, it is not running and no values are put into verifiedCards[].

So my problem is, even when there are duplicates they are not re-drawn.

Example output:

Not Verified 7 of Clubs Queen of Spades 7 of Clubs 7 of Spades Ace of Spades

Verified null null null null null

As shown, 7 of Clubs is a duplicate and the card should have been re-drawn, but it was not, and I can't figure out why.
Thanks.

Aucun commentaire:

Enregistrer un commentaire