意外错误

时间:2012-02-16 07:30:14

标签: java stack

我有一个学校项目,我不应该从星期一开始,并且不会再持续6周或更长时间。我们在java中创建一个程序来添加我们想要的数据库,我选择了游戏(标题,流派,平台,价格,数量(字符串,组合框,组合框,双,int)),我正在使用堆栈添加所有的对象,但由于某种原因我不能让它编译由于某种原因,我不断得到真正奇怪的错误。我的错误和代码分别在下面。

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

3 个答案:

答案 0 :(得分:3)

这个电话:

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

答案 1 :(得分:2)

您需要先定义一个方法,然后在方法中编写相关代码。我没有透露太多,因为这是一个家庭工作项目。尝试阅读有关java的类和方法的更多信息

答案 2 :(得分:1)

您不能直接在类体中使用代码,必须将其放入方法或构造函数中。