2013-02-26 251 views
1

在我的主要方法,日食是給我「DeckOfCards不能被解析爲一個變量」試圖創建一個新的平臺Eclipse的「不能被解析爲變量」

這開始的時候我是組合多個發生時.java文件合併爲一個更便於攜帶。

我失去的睡眠試圖解決這一問題...請幫助

import java.util.Random; 

public class PokerGame { 

    class Card { 

     private String face; // face of card ("Ace", "Deuce", ...) 
     private String suit; // suit of card ("Hearts", "Diamonds", ...) 

     // two-argument constructor initializes card's face and suit 
     public Card(String cardFace, String cardSuit) { 

      face = cardFace; // initialize face of card 
      suit = cardSuit; // initialize suit of card 
     } // end two-argument Card constructor 

     // return String representation of Card 
     public String toString() { 
      return face + " of " + suit; 
     } // end method toString 
    } 

    class DeckOfCards { 

     private Card[] deck; // arrays of Card objects 
     private int currentCard; // index of next Card to be dealt (0-51) 
     private static final int NUMBER_OF_CARDS = 52; // constant # of Cards 
     // random number generator 
     private final Random randomNumbers = new Random(); 

     // constructor fills deck of Cards 
     public DeckOfCards() { 
      String[] faces = { 
       "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" 
      }; 
      String[] suits = { 
       "Hearts", "Diamonds", "Clubs", "Spades" 
      }; 

      deck = new Card[NUMBER_OF_CARDS]; // create array of Card objects 
      currentCard = 0; // set currentCard so first Card dealt is deck[ 0 ] 

      // populate deck with Card objects 
      for (int count = 0; count < deck.length; count++) 
      deck[count] = new Card(faces[count % 13], suits[count/13]); 
     } // end DeckOfCards constructor 

     // shuffle deck of Cards with one-pass algorithm 
     public void shuffle() { 
      // after shuffling, dealing should start at deck[ 0 ] again 
      currentCard = 0; // reinitialize currentCard 

      // for each Card, pick another random Card (0-51) and swap them 
      for (int first = 0; first < deck.length; first++) { 
       // select a random number between 0 and 51 
       int second = randomNumbers.nextInt(NUMBER_OF_CARDS); 

       // swap current Card with randomly selected Card 
       Card temp = deck[first]; 
       deck[first] = deck[second]; 
       deck[second] = temp; 
      } // end for 
     } // end method shuffle 

     // deal one Card 
     public Card dealCard() { 
      // determine whether Cards remain to be dealt 
      if (currentCard < deck.length) return deck[currentCard++]; // return current Card in array 
      else return null; // return null to indicate that all Cards were dealt 
     } // end method dealCard 

    } 
    public static void main(String[] args) { 
     Card[] hand1, hand2; 
     PokerGame.DeckOfCards myDeckOfCards = DeckOfCards.new DeckOfCards(); 
     myDeckOfCards.shuffle(); // place Cards in random order 

     hand1 = new Card[5]; 
     hand2 = new Card[5]; 

     // print 5 Cards in the order in which they are dealt 
     for (int i = 0; i < 10; i++) { 

      if (i == 0) //display hand labels 
      System.out.printf("Hand 1: \t Hand 2: \n"); 

      if (i < 5) { 
       hand1[i] = myDeckOfCards.dealCard(); 
       System.out.printf("%-19s", hand1[i]); 
      } 
      if (i >= 5) { 
       hand2[i - 5] = myDeckOfCards.dealCard(); 
       System.out.printf("%-19s", hand2[i - 5]); 
      } 

      if ((i + 1) % 2 == 0) // output a newline after every other card 
      System.out.println(); 
     } // end for 
    } 

} 

回答

0

這班沒怎麼內在的工作。

首先,您需要創建PokerGame實例的實例:

PokerGame game = new PokerGame(); 
DeckOfCards myDeckOfCards = game.new DeckOfCards(); 
... 
+0

非常感謝。作品像魅力=] – 2013-02-26 15:40:00

0
DeckOfCards.new DeckOfCards(); 

這是不對的。嘗試new PokerGame.DeckOfCards()或者只是new DeckOfCards();

2

變化

PokerGame.DeckOfCards myDeckOfCards = DeckOfCards.new DeckOfCards(); 

PokerGame.DeckOfCards myDeckOfCards = new PokerGame.DeckOfCards(); 
+0

實際上'新的PokerGame.new DeckOfCards()'因爲它們是非靜態的內部類。 – Mordechai 2013-02-26 07:06:40

1

你兩類CardDeckOfCards應該是static,即:

static class Card { 
    ... 
} 

static class DeckOfCards { 
    ... 
} 

否則,你需要你的PokerGame的實例類創建它們,這是一個設計缺陷,因爲PokerGame沒有「狀態」(沒有字段/變量)。

實際上,他們應該是自己的top = level類(在他們自己的java文件中)。

0

移動卡和DeckOfCards定義PokerGame類定義之外,像這樣

public class PokerGame { 
    //your code goes here 
} 

class Card { 
    //your code goes here 
} 

class DeckOfCards{ 
    //your code goes here 
} 

這解決了你內心的類實例化的問題,但它不是一個好的設計。 類定義應該位於它們各自的文件中,以便稍後將更改合併。