我目前正在进行一场扑克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
是一个单独的类。
答案 0 :(得分:2)
没有理由在指示错误的地方无法分配变量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" };
答案 1 :(得分: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" };