【问题标题】:Syntax error, insert "{" to complete EnumBody (at end of class)语法错误,插入“{”以完成 EnumBody(在课程结束时)
【发布时间】:2018-04-28 02:19:15
【问题描述】:
package checkers;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JPanel;


enum Job{SPAWN, KING, NORM};
enum myColor{RED, BLACK};

int tileRow;
int tileCol;
Job job;
myColor side;
JButton button;
Checker piece;
Color color;

public class Tile 
{
    public Tile(int posRow, int posCol, JPanel panel, ActionListener listener)
    {
        int tileRow = posRow;
        int tileCol = posCol;
        if(tileRow > 9 || tileRow < 1)
            job = Job.KING;
        else if(tileRow < 4 || tileRow > 6)
            job = Job.SPAWN;
        else
            job = Job.NORM;

        button = new JButton();
        button.setPreferredSize(new Dimension(83, 83));
        if(tileRow%2==0)
        {
            if(tileCol%2==0)
            {
                color = Color.BLACK;
            }
            else
                color = Color.RED;
        }
        else
        {
            if(tileCol%2!=0)
            {
                color = Color.BLACK;
            }
            else
                color = Color.RED;
        }
        button.addActionListener((java.awt.event.ActionListener) listener);
    }

    public void Reset()
    {

    }

    public boolean isClicked(Object source)
    {
        if(source == button)
            return true;
        else 
            return false;
    }




}

编辑我编辑了整个代码体。就 Eclipse 所知,myColor 右大括号“应该”在 classBody 之后。

Eclipse 想要我删除 myColor 的右大括号,并用分号替换它;无论我是否放置分号,Eclipse 都会告诉我右大括号不应该存在,如果我删除它,则将我的 classBody 右大括号读取为 EnumBody 右大括号。

我不知道到底发生了什么,但这肯定会导致类内发生奇怪的事情(为跳棋游戏制作一个 Tile 类+对象)。

奇怪的是,如果我希望 Eclipse 将 Tile 读取为没有错误,我无法从另一个类中创建 Tile 对象数组。

【问题讨论】:

  • NORM 之后添加分号 BLACK。之后在这里工作正常。
  • 您能提供完整的代码吗?您可能会在其他地方遗漏 }{。
  • @GeorgeZougianos 我可以,是的
  • @ElliottFrisch 我在输入 stackoverflow 时忘记添加分号。哎呀。让他们在日食中。

标签: java syntax enums


【解决方案1】:

不完全确定您收到的错误消息。下面的例子EnumIssue.java 工作正常:

public class EnumIssue {

    enum Job
    {
        SPAWN, KING, NORM
    }
    enum myColor
    {
        RED, BLACK
    }

    public static void main(String[] args) {
        Job j = Job.SPAWN;
        myColor c = myColor.BLACK;
        System.out.println(j);
        System.out.println(c);
    }
}

输出:

SPAWN
BLACK

在提供有问题的整个代码后添加:

移动类Tile中的变量声明。更新后的sn-p如下:

:
:
import javax.swing.JPanel;

enum Job{SPAWN, KING, NORM};
enum myColor{RED, BLACK};

//This is where current variable declarations are. Move them inside class.

public class Tile
{
    //This is where variable declarations are moved to. 
    int tileRow;
    int tileCol;
    Job job;
    myColor side;
    JButton button;
    Checker piece;
    Color color;

    public Tile(int posRow, int posCol, JPanel panel, ActionListener listener)
    {
        int tileRow = posRow;
        :
        :

【讨论】:

  • 我将整个代码复制并粘贴到 OP 中。今天我又花了一个小时混合括号和分号,然后才想起我曾在这里发帖。据我所知,没有什么能解决我的所有问题。
  • 查看问题描述中发布的代码后更新了答案。
  • ...这真是令人难以置信的尴尬。我不知道我是怎么错过的。
猜你喜欢
  • 2012-10-21
  • 2017-07-30
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
相关资源
最近更新 更多