2012-02-16 180 views
0

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

[email protected]:~/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); 
    } 
} 
+0

添加了作业标签。 – 2012-02-16 07:38:45

+0

它在那里,但有些人删除它。 – 2012-02-16 08:06:54

回答

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

男人我讨厌它,当我做这样的愚蠢的错误;我想所有的学习过程。谢谢你,先生! :) – 2012-02-16 07:33:44

+0

它确实是学习过程的一部分。我想我们大多数人也是这样做的...... – MByD 2012-02-16 07:34:21

2

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

1

你不能直接在你的代码中加入你的代码,你必须把它放到一个方法或构造函数中。