方法不改变变量

时间:2013-12-03 19:44:08

标签: 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 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;

    }
}

这基本上是当玩家决定“击中”时被调用的方法,它需要持有玩家牌的阵列,玩家得分然后这个我称之为间距的东西,这基本上会转移卡片精灵-20向左停止然后落在屏幕右侧,看起来很难看。

现在,当我尝试调用此方法时:

hit(playerCards, playerScore, playerSpacing);

为什么playerScore和playerSpacing没有更新?卡片被添加得很好,因为它们绘制了受人尊敬的Sprite,我错过了什么?

3 个答案:

答案 0 :(得分:2)

Java始终是按值传递的。您正在更改本地变量scorespacing,但这些变量是playerScoreplayerSpacing的副本。

但为什么更改为cards更新playerCards?因为对象引用是按值传递的。您有一个引用的副本,它引用同一个对象。对象已更改(可以通过playerCardscards更改)。如果您已完成cards = new Array<Card>();,那么当您更改cards时,您将为本地变​​量playerCards提供新值,而cards将保持不变。

答案 1 :(得分:1)

这是固定代码:

持有自己的手,得分和间距的玩家对象:

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;

    }
}

一切都按预期工作,但现在我的代码是可重用的,这就是我写这个方法的原因。由于经销商将使用相同的代码进行播放。谢谢你们。

答案 2 :(得分:0)

值必须更新但反映回调用语句,因为在java中,所有内置数据类型都是按值传递的,所有类对象都是通过引用传递的,因此在调用后重新使用它时没有任何效果声明。将内置数据类型替换为代码中的各个类,int -> Integerfloat -> Float,它应该可以正常工作。