简体   繁体   中英

Why am I getting the Error: unexpected type required: variable found: value

I am trying to create a shuffle class for the cards dynamic array I the cards array contains strings for suit and rank and int for value

here is where im getting the error required: variable found: value

private void Shuffle() { 
    for (int p  = 0; p <1000; p++) {
        int rand1 = (int) (Math.random()* 52);
        int rand2 = (int) (Math.random()*52);
        Card Temp = cards.get(rand1);
        cards.get(rand1) = cards.get(rand2);
        cards.get(rand2) = Temp;
    }
}

I believe you are trying to shuffle cards 1000 times randomly, soo the most simplistic approach IMHO would be something like this (assuming your cards are in ArrayList)

private void Shuffle() {
    for (int p = 0; p < 1000; p++) {
        int rand1 = (int) (Math.random() * 52);
        int rand2 = (int) (Math.random() * 52);
        Card card1 = cards.get(rand1);
        Card card2 = cards.get(rand2);
        cards.set(rand1, card2);
        cards.set(rand2, card1);
    }
}

I believe, cards is of type List. cards.get(index) returns an object of the Card. An object can be assigned to a reference and not to another object. Hence, cards.get(rand1) = cards.get(rand2) is like assigning an object to another object. It is similar to writing 1=2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM