接收未声明的类的编译器错误,然后接收重复的类

时间:2016-04-05 04:35:44

标签: java class compiler-errors

我刚刚创建了一个程序,它创建了一个卡片对象数组,将它们随机播放并打印出来。但是我得到了以下错误,作为一名学生,我很难理解为什么。

错误:

  

" HW6GetBig.java:23:错误:类卡是公开的,应该声明   一个名为Card.java的公共类卡{"

     

" HW6GetBig.java:39:错误:类DeckOfCards是公开的,应该是   在名为DeckOfCards.java的文件中声明公共类DeckOfCards {"

     

" HW6GetBig.java:11:错误:找不到符号           DeckOfCards myDeckofCards = new DeckofCards();"

到目前为止,我已经陷入困境,不知道为什么这个班级会成为未宣布的。如果我在netbeans中将文件名更改为Card,我仍然会收到错误,指出已找到重复的类。请帮忙。

非常感谢,                       - 协同作用

 class HW6GetBig
    {
        public static void main(String[] args)
        {
            DeckOfCards myDeckofCards = new DeckofCards();
            myDeckofCards.deckShuffler(); // Randomizes / Shuffles the Cards in the Deck, using a Random # and Swaps

            // Print 52 Cards in the Order in which they are dealt
            for (int i = 0; i < 13; i++) {
                // Deal and Print 4 Cards
                System.out.printf("%-20s%-20s%-20s%-20s\n",
                myDeckofCards.dealCard(), myDeckofCards.dealCard(),
                myDeckofCards.dealCard(), myDeckofCards.dealCard());
            }
        }
    }
    public class Card {
        //Card Class represents a Virtual Playing Card in our Deck
        private String rank; // Ranks Of Card
        private String suit; // Suit Of Card

        // Two Argument Constructor initializes card's face and suit
        public Card(String cardRank, String cardSuit) {
            rank = cardRank; // Intializing Values of Card's Rank
            suit = cardSuit; // Intializing Values of Card's Suit
        }
        // Return String representing the Card
        public String CardToString() {
            return rank + " of " + suit;
        }

    }
    public class DeckOfCards {
        private Card deck[]; //Declaration of Array of Card Objects
        private int topCard; //Card to be Dealt
        private final int NUM_CARDS = 52; // Constant # Of Cards in a Standard Deck
        int random = (int)(Math.random()*52+1);

        public DeckOfCards() {
            String ranks[] = {"Ace","Two","Three","Four","Five","Six","Seven",
            "Eight","Nine","Ten","Jack","Queen","King"};
            String suits[] = {"Spades","Hearts","Clubs","Diamonds"};

            deck = new Card[NUM_CARDS]; // Creating an Array of Card Objects
            topCard = 0; // Setting Current Card so 1st Card is deck[0]
            // Filling the Seats of our Empty Deck Room with Card Objects
            for (int i = 0; i < deck.length; i++) {
                deck[i] = new Card(ranks[i % 13], suits[i / 13]);
                // End of Deck Of Cards Constructor
            }
        }
            public void deckShuffler() {
                // After Shuffling, The Deck should begin at deck[0] once again
                topCard = 0;
                // For each Card, pick a "Random Card/#" and Swap.
                for(int i = 0; i < deck.length; i++) {
                    // Obtaining a Random # Between 0 and 51.
                    int j = random;
                    // Swapping Currently Selected Card (i) with Random Card (j)
                    Card tempObject = deck[i];
                    deck[i] = deck[j];
                    deck[j] = tempObject;
                }
            }
                public Card dealCard() {
                    // Ensure the Top Card does not exceed the Deck Length
                    if (topCard < deck.length) {
                    return deck[topCard++];
                    } else {
                    return null;
                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

错误消息说明了一切:

  

类卡是公共的,应该在名为Card.java的文件中声明   class DeckOfCards是public,应该在一个名为的文件中声明   DeckOfCards.java

以下是您的选择:

  • 将这两个类移动到他们自己的文件中,如错误消息中所示。
  • 从类声明中删除public修饰符。

请参阅Java语言规范中的相关部分7.6. Top Level Type Declarations

  

当且仅当包存储在文件系统(第7.2节)中时,主机   系统可以选择强制执行编译时的限制   如果在由...组成的名称下的文件中找不到类型,则会出错   类型名称加上扩展名(例如.java.jav),如果有的话   以下是真的:

     
      
  • 该类型由声明类型的包的其他编译单元中的代码引用。

  •   
  • 该类型声明为public(因此可以从其他包中的代码访问)。

  •