我有一个学校项目要到周一才能开始,而且还要过6个星期或者更长时间才能交完。我们用java创建了一个程序,将我们想要的添加到数据库中,我选择了游戏(标题,风格,平台,价格,数量(字符串,组合框,组合框,双精度,整型)),我使用一个堆栈来添加所有的对象,但由于某些原因,我无法编译它,并且我一直收到非常奇怪的错误。我的错误和代码分别在下面。
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);
}
}发布于 2012-02-16 15:32:41
此调用:
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);
}
}发布于 2012-02-16 15:33:02
您需要首先定义一个方法,然后在该方法中编写相关代码。我没有透露太多,因为这是一个家庭工作项目。尝试阅读更多关于java的类和方法的内容
发布于 2012-02-16 15:36:41
您不能将代码直接放在类主体中,必须将其放入方法或构造函数中。
https://stackoverflow.com/questions/9307192
复制相似问题