我把這段代碼放在這裏 - PlayingDeck
類。它沒有完成,正如你所看到的,但是會發生的是,在第17行右邊的for循環中,我收到一條錯誤消息,說明PlayingCard
聲明和初始化不是一個聲明。爲什麼這樣?當我使用相同的語句將大括號添加到for循環時,錯誤消息消失。有人能解釋爲什麼嗎?我需要的只是兩個括號,一個在開始處,另一個在for循環結尾......?for循環語法有什麼問題?
謝謝
無法編譯--------代碼,編譯
public class PlayingDeck {
static int numberOfCards;
static {
numberOfCards = 52;
}
PlayingCard[] playingDeckArray;
PlayingDeck() {
playingDeckArray = new PlayingCard[numberOfCards];
for (int currentCardNumber = 0; currentCardNumber > 51; currentCardNumber++)
PlayingCard currentCard = playingDeckArray[currentCardNumber];
}
public static void main(String[] args) {
}
}
代碼-----
public class PlayingDeck {
static int numberOfCards;
static {
numberOfCards = 52;
}
PlayingCard[] playingDeckArray;
PlayingDeck() {
playingDeckArray = new PlayingCard[numberOfCards];
for (int currentCardNumber = 0; currentCardNumber > 51; currentCardNumber++){
PlayingCard currentCard = playingDeckArray[currentCardNumber];
}
}
public static void main(String[] args) {
}
}
它是公平地說,它有一個範圍,但該範圍是沒用? – robbmj 2014-11-23 06:53:09
甚至將條件更改爲'currentCardNumber
2014-11-23 06:53:43
我想這是有道理的,因爲當我這樣做循環時,一切都編譯得很好: 'PlayingDeck(){ DeckArray = new PlayingCard [numOfSuits] [numOfRanks]; 對(INT currentSuitNumber = 1; currentSuitNumber <= numOfSuits; currentSuitNumber ++){ 對(INT currentRankNumber = 1; currentRankNumber <= numOfRanks; currentRankNumber ++) DeckArray [currentSuitNumber - 1] [currentRankNumber - 1] =新遊戲牌(currentSuitNumber,currentRankNumber ); } }' 但是這裏沒有變量聲明,所以我猜花括號是沒有必要的。謝謝。 – nhooyr 2014-11-23 20:10:27