在循环内声明时无法解析局部变量,但在外部声明时抛出NPE

时间:2013-04-11 17:38:33

标签: nullpointerexception declaration

基本上我正在做的是通过一副牌来耐心测试,以确定耐心耗尽的概率,主要方法只打印出这个数字,没有用户系统输入。当第一张牌被抽出时,你说“1”,第二张牌你说“2”,第三张牌“3”,然后你从第四张牌开始。所以它是1-2-3-1-2-3-1 -...如果你说一个ace,当你说1,当你说2或3时你说3,那么耐心耗尽。这是通过一副52张卡片共进行100次(也许我会将其设置为1000张)。下面是两个类卡(创建卡)和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?

事先谢谢你。

1 个答案:

答案 0 :(得分: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;
        }

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