【问题标题】:Eclipse "can not be resolved to variable"Eclipse“无法解析为变量”
【发布时间】:2013-02-26 05:18:03
【问题描述】:

在我的主要方法中,eclipse 在尝试创建新牌组时给了我“DeckOfCards 无法解析为变量”

当我将多个 .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
    }

}

【问题讨论】:

    标签: java eclipse variables declare


    【解决方案1】:

    改变

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

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

    【讨论】:

    • 其实new PokerGame.new DeckOfCards() 因为它们是非静态内部类。
    【解决方案2】:

    你的两个类CardDeckOfCards应该是static,即:

    static class Card {
        ...
    }
    
    static class DeckOfCards {
       ...
    }
    

    否则,您需要 PokerGame 类的实例来创建它们,这是一个设计缺陷,因为 PokerGame 没有“状态”(没有字段/变量)。

    实际上,它们应该是自己的顶级类(在自己的 java 文件中)。

    【讨论】:

      【解决方案3】:

      内部类不是这样工作的。

      您首先需要创建PokerGame 实例的实例:

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

      【讨论】:

      • 非常感谢。像魅力一样工作=]
      【解决方案4】:
      DeckOfCards.new DeckOfCards();
      

      这是错误的。试试new PokerGame.DeckOfCards() 或只是new DeckOfCards();

      【讨论】:

        【解决方案5】:

        在 PokerGame 类定义之外移动 Card 和 DeckOfCards 定义,像这样

        public class PokerGame {
           //your code goes here
        }
        
        class Card {
           //your code goes here
        }
        
        class DeckOfCards{
           //your code goes here
        }
        

        这解决了您的内部类实例化问题,但这不是一个好的设计。 类定义应该在各自的文件中,以便以后更容易合并更改。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-09-19
          • 2015-04-29
          • 1970-01-01
          • 2014-12-01
          • 2014-08-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多