[英]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.