Java 方法不改变变量
我这里有点问题,真的,我不明白为什么。我已经写了很多代码,在参数中使用字段变量,然后对其进行更改。方法如下:Java 方法不改变变量,java,methods,Java,Methods,我这里有点问题,真的,我不明白为什么。我已经写了很多代码,在参数中使用字段变量,然后对其进行更改。方法如下: public void hit(Array<Card> cards, int score, float spacing) { // Take the last card from the deck and store it in a temp card variable nextCard = deck.pop(); // If the card val
public void hit(Array<Card> cards, int score, float spacing) {
// Take the last card from the deck and store it in a temp card variable
nextCard = deck.pop();
// If the card value is equal to 11, it must be an ace, if it puts the
// player over 21 it makes the ace have a value of 1
if ((nextCard.getCardRealValue() == 11)
&& nextCard.getCardRealValue() + score > 21) {
nextCard.setCardRealValue(1);
}
// Add the card to the array passed in the parameter
cards.add(nextCard);
// Check the last card in the array (the one just added) grab it's value, add it to total
score += cards.peek().getCardRealValue();
// Shrink the main deck, not sure if neccessary but not point in leaving empty memory
deck.shrink();
// Move the sprites 20 pixels
spacing -= 20;
// tbh this bit never gets called, stupid useless code lol
if (score > 21) {
if (cards.peek().getCardRealValue() == 11) {
cards.peek().setCardRealValue(1);
score -= 10;
}
}
// if the first card has not been checked and score is over 21, if it is an ace change it to a value of 1
if (!aceOne && score > 21 && cards.get(0).getCardRealValue() == 11) {
cards.get(0).setCardRealValue(1);
score -= 10;
aceOne = true;
// Same as above, just if second card is ace
} else if (!aceTwo && score > 21
&& cards.get(1).getCardRealValue() == 11) {
cards.get(1).setCardRealValue(1);
score -= 10;
aceTwo = true;
}
}
为什么playerScore和playerSpacing没有更新?这些卡被添加得很好,因为它们绘制了受人尊敬的精灵,我是不是遗漏了什么?Java总是按值传递。您正在更改局部变量得分和间距,但这些是playerScore和playerSpacing的副本
但是为什么卡片的变化会更新玩家卡呢?因为对象引用是按值传递的。您有引用同一对象的引用的副本。该对象已更改,可以通过玩家卡或卡片进行更改。如果您已经完成了cards=new Array;,然后,您将为局部变量cards指定一个新值,并且当您更改卡片时,playerCards将保持不变。这些值必须更新,但会反映回调用语句,因为在java中,所有内置数据类型都是按值传递的,所有类对象都是按引用传递的,因此,在调用语句后重新使用它时没有效果。将内置数据类型替换为代码中的各个类,int->Integer和float->float,这样应该可以工作。下面是固定代码: 持有自己手牌、分数和间距的玩家对象:
public class Player {
int score = 0;
float spacing = 390;
Array<Card> hand = new Array<Card>();
public Player(){
hand.ordered = false;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public float getSpacing() {
return spacing;
}
public void setSpacing(float spacing) {
this.spacing = spacing;
}
public Array<Card> getHand() {
return hand;
}
public void addToHand(Card card){
hand.add(card);
}
}
新方法:
public void hit(Array<Card> cards, Player player){
nextCard = deck.pop();
if ((nextCard.getCardRealValue() == 11)
&& nextCard.getCardRealValue() + player.getScore() > 21) {
nextCard.setCardRealValue(1);
}
cards.add(nextCard);
player.setScore(player.getScore() + player.getHand().peek().getCardRealValue()) ;
deck.shrink();
player.setSpacing(player.getSpacing() - 20);
cardDeal.play();
if (player.getScore() > 21) {
if (cards.peek().getCardRealValue() == 11) {
cards.peek().setCardRealValue(1);
player.setScore(player.getScore() - 10);
}
}
if (!aceOne && player.getScore() > 21
&& cards.get(0).getCardRealValue() == 11) {
cards.get(0).setCardRealValue(1);
player.setScore(player.getScore() - 10);
aceOne = true;
} else if (!aceTwo && player.getScore() > 21
&& cards.get(1).getCardRealValue() == 11) {
cards.get(1).setCardRealValue(1);
player.setScore(player.getScore() - 10);
aceTwo = true;
}
}
除了现在我的代码是可重用的,这就是我编写这个方法的原因。因为经销商将使用相同的代码进行播放。谢谢大家。据我所知,Java总是按值传递,这不是真正的语句。你能给我一个参考吗?@bluebrain请看。谢谢你的回答,另一个回答说我应该用Integer和Float等对象替换我的数据类型,而不是int/Float。然而,这并没有改变任何事情,有什么想法吗?@bluebrain一个物体根本不会通过。对对象的引用被传递,而该引用是通过值传递的。@Gibbo这不会有帮助,因为那些包装器类型是不可变的。您可以创建自己的对象来保存原始值“分数”和“间距”。将对该对象的引用传递到hit方法中。然后你可以用setter方法来改变这些值。好吧,我明白了,但是这确实改变了一件事,它仍然没有更新分数和间距。
public void hit(Array<Card> cards, Player player){
nextCard = deck.pop();
if ((nextCard.getCardRealValue() == 11)
&& nextCard.getCardRealValue() + player.getScore() > 21) {
nextCard.setCardRealValue(1);
}
cards.add(nextCard);
player.setScore(player.getScore() + player.getHand().peek().getCardRealValue()) ;
deck.shrink();
player.setSpacing(player.getSpacing() - 20);
cardDeal.play();
if (player.getScore() > 21) {
if (cards.peek().getCardRealValue() == 11) {
cards.peek().setCardRealValue(1);
player.setScore(player.getScore() - 10);
}
}
if (!aceOne && player.getScore() > 21
&& cards.get(0).getCardRealValue() == 11) {
cards.get(0).setCardRealValue(1);
player.setScore(player.getScore() - 10);
aceOne = true;
} else if (!aceTwo && player.getScore() > 21
&& cards.get(1).getCardRealValue() == 11) {
cards.get(1).setCardRealValue(1);
player.setScore(player.getScore() - 10);
aceTwo = true;
}
}