-1

我目前正在做一个扑克 java 游戏练习,我遇到了这个我无法理解的丑陋错误。这一定是某种语法错误,因为编译器会抛出其中的 100 个错误。但我只是不知道错误在哪里??

import java.util.Random;

public class Poker
{
    private Card[] deck;
    private Card[] hand;
    private int currentCard;
    private int numbers[], triples, couples;
    private String faces[], suits[];

    private boolean flushOnHand, fourOfAKind, isStraight;
    private static final int NUMBER_OF_CARDS = 52;

    private static final Random randomNumbers = new Random();

    Poker()
    {
        faces = { "Ace", "Deuce", "Three", "Four", "Five",
            "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
        suits = { "Hearts", "Diamonds", "Clubs", "Spades" };

        numbers = new int[ 13 ];

        deck = new Card[ NUMBER_OF_CARDS ];

        triples = 0; // right here's where the compiler starts complaining
        couples = 0;
            ...

虽然我无法发现任何语法错误?

顺便说一句,这Card是一个单独的类。

4

3 回答 3

2

没有理由triples不能在您指示错误的地方分配变量。

但是,您需要像这样分配字符串数组:

faces = new String[] { "Ace", "Deuce", "Three", "Four", "Five",
         "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
suits = new String[] { "Hearts", "Diamonds", "Clubs", "Spades" };
于 2012-09-09T20:22:47.217 回答
1

您初始化数组值的语法不正确。

我不确定为什么编译器在开始抱怨之前要等待另外四行,但是您的数组声明在上面的几行中是无效的。

    faces = { "Ace", "Deuce", "Three", "Four", "Five",
        "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    suits = { "Hearts", "Diamonds", "Clubs", "Spades" };

应该是这样的...

    faces = new String[] { "Ace", "Deuce", "Three", "Four", "Five",
        "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    suits = new String[] { "Hearts", "Diamonds", "Clubs", "Spades" };

如果您在声明它的同一行初始化它,则只能使用第一个结构,如下所示:

private String faces[] = { "Ace", "Deuce", "Three", "Four", "Five",
        "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
于 2012-09-09T20:22:59.320 回答
1

我意识到这里已经接受了一个答案,但我想指出,如果变量不依赖于构造函数的任何参数,那么在声明时初始化变量会更容易,即

public class Poker
{
    private static final int NUMBER_OF_CARDS = 52;
    private Card[] deck = new Card[ NUMBER_OF_CARDS ];
    private int numbers[] = new int[ 13 ]
    private int triples = 0;
    private int couples = 0;
    private String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
                               "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    private String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };

等等

于 2012-09-09T20:57:42.613 回答