0

基本上我正在做的是通过一副纸牌进行耐心测试,以确定耐心耗尽的概率,主要方法只打印出这个数字,没有用户系统输入。当第一张牌被抽出来时,你说“1”,第二张牌说“2”,第三张牌“3”,然后你从第四张牌重新开始。所以是 1-2-3-1-2-3-1-... 如果你说 1 时抽到 A,你说 2 时抽到 2,或者说 3 时抽到 3,那么耐心就用完了。这是通过一副 52 张纸牌总共运行 100 次(也许我会将其设置为 1000)。下面是两个类 Card(创建卡片)和 Carddeck(创建卡片组)以及主要方法 d84 的实现。

public class Card {
public static final int SPADES = 1;
public static final int HEARTS = SPADES + 1;
public static final int DIAMONDS = SPADES + 2;
public static final int CLUBS = SPADES + 3;
private int rank;   // value
private int suit;   // color

/** Creates a card with color, suit (SPADES, HEARTS, DIAMONDS, CLUBS) 
    and value, rank (1-13) */
public Card(int suit, int rank) {
    this.suit = suit;
    this.rank = rank;
}

/** Gets suit */
public int getSuit() {
    return suit;
}

/** Gets rank */
public int getRank() {
    return rank;
}

/** Returns a readable representation of the card, e.g. "diamonds jack" */
public String toString(){
    String suitString = "";     
    switch(suit) {
        case SPADES: suitString = "spades"; break;
        case HEARTS: suitString = "hearts"; break;
        case DIAMONDS: suitString = "diamonds"; break;
        case CLUBS: suitString = "clubs"; break;
    }
    String rankString = "";
    switch(rank) {
        case 1: rankString = "ace"; break;
        case 11: rankString = "jack"; break;
        case 12: rankString = "queen"; break;
        case 13: rankString = "king"; break;
        default: rankString = String.valueOf(rank);
    }
    return suitString + " " + rankString;
}
}

-

import java.util.Random;
public class CardDeck {
private Card[] theCards;
private int cardNbr = -1;
public CardDeck() {
    theCards = new Card[52];
    for (int suit = Card.SPADES; suit <= Card.CLUBS; suit++) {
        for (int r = 0; r < 13; r++) {
            theCards[r*suit] = new Card(suit, r+1);
        }
    }
}
public void shuffle() {
    Random rand = new Random();
    for (int i = 0; i < 52; i++) {
        int n = rand.nextInt(52);
        theCards[i] = theCards[n];
    }
}
public boolean moreCards() {
    return cardNbr < 51;
}
public Card getCard() {
    cardNbr++;
    return theCards[cardNbr];
}}

-

public class d84 {
public static void main(String[] args) {
    int patBroke = 0;
    for (int j = 0; j < 100; j++){
        CardDeck deck = new CardDeck();
        deck.shuffle();
        while (deck.moreCards() == true) {
            int say = 1;
            while (say <= 3) {
                Card drawn = deck.getCard();
                if (say == drawn.getRank()) {
                    patBroke++;
                    break;
                }
            say++;
            }
            if (say == drawn.getRank()) {
                break;
            }
        }
    }
    System.out.print("The probability of patience running out is " + (1-patBroke/100));
}
}

执行时,线程“main”java.lang.Error 中的异常:未解决的编译问题:在 d84.main(d84.java:17) 无法解决所绘制的问题。宣布了吗?删除此子句后,编译器会在 d84.java:11 处声明相同的错误。如果 Card draw 声明在循环外,则执行将在 17 或 11 处抛出 NullPointerException。

课堂卡片是在讲座中给出的,所以我不会在那里更改任何代码。另一方面,Carddeck 是我自己的实现,主要方法 d84 也是我自己的代码。

那么,在这种情况下,如何避免“绘制无法解决”和 NPE?

事先谢谢你。

4

1 回答 1

0

您的问题与循环本身无关,而是与{}子句中变量的范围有关。不能在声明它的范围之外引用变量。

将您的逻辑更改为:

        int say = 1;
        Card drawn = null;  // Added
        while (say <= 3) {
            drawn = deck.getCard();  // Changed
            if (say == drawn.getRank()) {
                patBroke++;
                break;
            }
            say++;
        }
        if (say == drawn.getRank()) {
            break;
        }

(但很可能还有其他问题。)

于 2013-04-11T17:43:44.453 回答