簡體   English   中英

意外錯誤

[英]Unexpected errors

我有一個學校項目,我不應該在周一之前開始,而且還要再等6個星期或更長時間。 我們正在用Java創建一個程序,將想要的內容添加到數據庫中,我選擇了游戲(標題,類型,平台,價格,數量(字符串,組合框,組合框,double,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);
    }
}

這個電話:

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的類和方法的更多信息

您不能將代碼直接放在類主體中,必須將其放入方法或構造函數中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM