java - 意外错误

标签 java stack

我有一个学校项目,我不应该在周一之前开始,而且还要 6 周或更长时间才能完成。我们正在用java创建一个程序来将我们想要的内容添加到数据库中,我选择了游戏(标题,流派,平台,价格,数量(字符串,组合框,组合框, double ,整数)),我使用堆栈来添加所有对象,但由于某种原因我无法编译它,并且我不断收到非常奇怪的错误。我的错误和代码分别如下。

nathan@ubuntu:~/Desktop/TAFE/Jeff (Java)/personalProject$ javac *.java
GameCombo.java:11: <identifier> expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                   ^
GameCombo.java:11: illegal start of type
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                    ^
GameCombo.java:11: ')' expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                       ^
GameCombo.java:11: ';' expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                            ^
GameCombo.java:11: illegal start of type
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                             ^
GameCombo.java:11: <identifier> expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                            ^
GameCombo.java:11: ';' expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                             ^
GameCombo.java:11: illegal start of type
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                               ^
GameCombo.java:11: <identifier> expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                                ^
GameCombo.java:11: <identifier> expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                                   ^
GameCombo.java:11: illegal start of type
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                                          ^
GameCombo.java:11: <identifier> expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                                           ^
GameCombo.java:11: ';' expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                                             ^
GameCombo.java:11: illegal start of type
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                                              ^
GameCombo.java:11: <identifier> expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                                               ^
GameCombo.java:11: ';' expected
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
                                                                ^
16 errors

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.io.*;
import java.util.*;

public class GameCombo extends JPanel {
  ArrayList<Game> gamesList = new ArrayList<Game>();
  Stack<Game> gamesStack = new Stack<Game>();
    gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
    //gamesList.add(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
    //gamesList.add(new Game("[Dead Space]", "Xbox 360", "Horror", "$68.00", "1"));

  //String[] games = {"", "[Halo: Reach] Xbox 360; Action; $108.00; 2;", "[Dead Space] Xbox 360; Horror; $65.00; 1;"}; 
  private JComboBox _gameBox = new JComboBox(gamesStack);

    public GameCombo() {
      setLayout(new GridLayout(1,1,1,1));

        add(_gameBox);
    }
}

最佳答案

此调用:

gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));

应该在某种方法中,因为您想在每个新对象中执行此操作,正如您的代码所示,为什么不将其移至构造函数:

public class GameCombo extends JPanel {
  ArrayList<Game> gamesList = new ArrayList<Game>();
  Stack<Game> gamesStack = new Stack<Game>();
  private JComboBox _gameBox;

    public GameCombo() {
         setLayout(new GridLayout(1,1,1,1));
         gamesStack.push(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
         _gameBox = new JComboBox(gamesStack);
         add(_gameBox);
    }
}

关于java - 意外错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9307192/

相关文章:

java - 无法使用准备好的语句插入数据库

java - Java中无需排序即可查找众数和众数频率

java - com.google.gson.JsonSyntaxException : Expected BEGIN_ARRAY but was STRING

Java Stack peek 方法显示 0 而不是正确的数字

java - Java 栈和递归

python - 以相反顺序打印队列中的小组数据

java - JOptionPane 将我的代码变成一个实际的游戏

java\r 字符在 Eclipse 中不起作用

algorithm - 最长递增子序列——线性时间解?

c++ - 带有模板的字符串堆栈?