簡體   English   中英

Java錯誤 <identifier> 預期

[英]Java error <identifier> expected

我目前正在從事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是一個單獨的類。

沒有理由不能在指示錯誤的位置分配變量triples

但是,您需要像這樣分配String數組:

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

您用於初始化數組值的語法不正確。

我不確定為什么編譯器在開始抱怨之前還要等待另外四行,但是您的數組聲明在以上幾行中無效。

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

我意識到這里已經接受了一個答案,但我想指出的是,如果變量不依賴於構造函數的任何參數,則在聲明時初始化變量會更容易

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

等等

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM