简体   繁体   English

为什么我收到错误:需要意外类型:找到变量:值

[英]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我正在尝试为卡片动态数组创建一个洗牌 class 我卡片数组包含花色和等级的字符串以及价值的 int

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)我相信你正在尝试随机洗牌 1000 次,所以恕我直言,最简单的方法就是这样(假设你的牌在 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. cards.get(index) 返回 Card 的 object。 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.一个 object 可以分配给一个引用而不是另一个 object。因此,cards.get(rand1) = cards.get(rand2) 就像将一个 object 分配给另一个 object。它类似于写 1=2。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到意外的类型、需要变量的错误? 即使我使用的是变量 - Why am I getting a unexpected type, variable required error? even though I am using a variable 错误:意外类型,必需:变量并找到:值 - Error: unexpected type, required: variable and found: value Java:设置字符串中字符位置的值时,我收到意外的类型错误(必填:变量;找到:值) - Java: I am receiving an unexpected type error(required: variable; found: value) when setting the value of a character location in a string 所需的Java错误意外类型:变量; 找到:ArrayList中的值 - Java Error unexpected type required: variable; found: value in an ArrayList 另一个作业问题错误意外类型是必需的:变量已找到:值 - another homework question error unexpected type required:variable found:value Java转换错误:意外类型。 必需:变量,找到:值 - Java casting error: unexpected type. required: variable, found: value Java错误消息。 意外类型,必需变量,发现值 - Java Error message. unexpected type, required variable, found value Java意外类型错误必需变量找到值 - Java unexpected type Error Required Variable Found Value Java编译错误:意外类型要求:找到变量:值 - Java Compile Error: Unexpected Type required: variable found: value 类运算符错误数组:所需的意外类型:找到的变量:值 - Array of Class Operator Error: unexpected type required: variable found: value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM