我有一个学校项目,我不应该在周一之前开始,而且还要 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/