0

我不明白为什么我会得到这样的输出。每次我抽出初始牌时,输出都会尝试重新创建和重新洗牌,但为什么呢?因为我正在检查卡片组是否为空或为空,如果是,则正在创建它。

为什么我的代码不起作用?

输出:

#########    BLACK    ###    JACK    #########

deal, hit, stay, exit: deal

Creating deck...
Shuffling deck...

Drawing Player's card... A_♦
Creating deck...
Shuffling deck...

Drawing Dealer's card... X_X
Creating deck...
Shuffling deck...

Drawing Player's card... 6_♥
Creating deck...
Shuffling deck...

Drawing Dealer's card... X_X

Dealers Hand: A_♦ X_X = 11
Players Hand: 6_♥ A_♦ = 17

初始绘制方法:

private static Deck initialDraw(Deck deck, Hand player, Hand dealer)
{
    drawFromDeck(deck, player);
    drawFromDeck(deck, dealer);
    drawFromDeck(deck, player);
    drawFromDeck(deck, dealer);

    System.out.print("\n");

    showHands(player, dealer);
    compareHands(player, dealer);

    return deck;
}

当我使用以下代码时,我没有得到这个输出,它有什么不同?我仍在引用同一个数组并将其传回,不是吗?

private static Deck initialDraw(Deck deck, Hand player, Hand dealer)
{
    deck = checkDeck(deck);

    drawFromDeck(deck, player);
    drawFromDeck(deck, dealer);
    drawFromDeck(deck, player);
    drawFromDeck(deck, dealer);

    System.out.print("\n");

    showHands(player, dealer);
    compareHands(player, dealer);

    return deck;
}
4

1 回答 1

1

快速解决方案

private static Deck initialDraw(Deck deck, Hand player, Hand dealer)

    {
        deck = drawFromDeck(deck, player);
        deck = drawFromDeck(deck, dealer);
        deck = drawFromDeck(deck, player);
        deck = drawFromDeck(deck, dealer);

        System.out.print("\n");

        showHands(player, dealer);
        compareHands(player, dealer);

        return deck;
    }

说明

在内部drawFromDeck,您正在创建/更新卡片组并返回它,但不更新变量,如initialDraw. 这可能不是这样的问题(因为对象是通过引用传递的,但是在依赖它之前进行测试!),但我假设您正在传递nullinto initialDraw,因此null正在传递给每个drawFromDeck调用,因此Deck每次都会创建一个新对象。

对于您的代码工作的编辑

在这种情况下,您要确保它deck不是null引用,而是对实际Deck对象的引用,因此您将向Deck每次drawFromDeck调用传递对对象的引用,而不是null每次都传递引用。

于 2012-12-06T04:31:01.247 回答