2012-02-16 218 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

你不能直接在你的代碼中加入你的代碼,你必須把它放到一個方法或構造函數中。