简体   繁体   English

Java错误 <identifier> 预期

[英]Java error <identifier> expected

I am currently working on a Poker java game exercise, and I get this ugly error which I'm not being able to understand. 我目前正在从事Java扑克游戏的练习,但遇到了这个丑陋的错误,我无法理解。 It must be some kind of syntax error since the compiler throws 100 of those. 由于编译器将抛出其中的100个,因此一定是某种语法错误。 But I just don't know where the error is?? 但是我只是不知道错误在哪里?

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;
            ...

though I am not able to spot any syntax error? 虽然我无法发现任何语法错误?

Btw, the Card is a separate class. 顺便说一句, Card是一个单独的类。

There's no reason why the variable triples can't be assigned where you are indicating the error. 没有理由不能在指示错误的位置分配变量triples

However, you need to assign the String arrays like so: 但是,您需要像这样分配String数组:

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

Your syntax for initializing the array values is incorrect. 您用于初始化数组值的语法不正确。

I'm not sure why the compiler is waiting an additional four lines before it starts complaining, but your array declaration is invalid several lines above. 我不确定为什么编译器在开始抱怨之前还要等待另外四行,但是您的数组声明在以上几行中无效。

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

Should be like this... 应该是这样的...

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

You can only use the first structure if you are initializing it on the same line you declare it, like this: 如果要在声明它的同一行上对其进行初始化,则只能使用第一个结构,如下所示:

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

I realise that an answer has already been accepted here but I wanted to point out that it's easier to initialise your variables at their point of declaration if they don't rely on any parameters of the constructor, ie 我意识到这里已经接受了一个答案,但我想指出的是,如果变量不依赖于构造函数的任何参数,则在声明时初始化变量会更容易

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" };

etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM