“非法表达式开始”
有人可以帮我调试这个请。我试图回到Java,所以我不是最好的错误。这似乎是一个简单的解决方法,但是我最后一次做Java是作为高中的新生,所以我不知道该怎么做。“非法表达式开始”
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JPanel.*;
import javax.swing.JLabel.*;
import java.util.*;
import java.text.*;
public class CharacterCreator extends JApplet {
public final String[] races = {"Human", "Dwarf"};
public JComboBox<String>charRace = new JComboBox<String>(races);
public static void main(String[]args)
{
public final String[] classes = {"Mage", "Warrior", "Ranger"};
public final JComboBox<String>charClass = new JComboBox<String>(classes);
int[] attPts = new int[7];
JLabel pointPool = new JLabel("" + attPts[0]);
public final JButton STRplus = new JButton("+");
public final JButton STRminus = new JButton("-");
JLabel strValue = new JLabel("" + attPts[1]);
public final JButton CONplus = new JButton("+");
public final JButton CONminus = new JButton("-");
JLabel conValue = new JLabel("" + attPts[2]);
public final JButton DEXplus = new JButton("+");
public final JButton DEXminus = new JButton("-");
JLabel dexValue = new JLabel("" + attPts[3]);
public final JButton INTELplus = new JButton("+");
public final JButton INTELminus = new JButton("-");
JLabel intelValue = new JLabel("" + attPts[4]);
public final String[] bonusAtt = {"Strength", "Constitution", "Dexterity",
"Intelligence"};
public JComboBox<String>humanAtt = new JComboBox<String>(bonusAtt);
}
public CharacterCreator() {
JPanel asPanel = new JPanel();
asPanel.setLayout(new GridLayout(6, 1));
asPanel.add(new JLabel("Strength"));
asPanel.add(new JLabel("Constitution"));
asPanel.add(new JLabel("Dexterity"));
asPanel.add(new JLabel("Intelligence"));
JPanel mpPanel = new JPanel();
mpPanel.setLayout(new GridLayout(6, 1));
mpPanel.add(strValue);
mpPanel.add(conValue);
mpPanel.add(dexValue);
mpPanel.add(intelValue);
JPanel abPanel = new JPanel();
abPanel.setLayout(new GridLayout(6, 2));
abPanel.add(STRplus);
abPanel.add(STRminus);
abPanel.add(CONplus);
abPanel.add(CONminus);
abPanel.add(DEXplus);
abPanel.add(DEXminus);
abPanel.add(INTELplus);
abPanel.add(INTELminus);
JPanel attributes = new JPanel();
attributes.add(new JLabel("Ability Score"), BorderLayout.NORTH);
attributes.add(asPanel, BorderLayout.WEST);
attributes.add(mpPanel, BorderLayout.CENTER);
attributes.add(abPanel, BorderLayout.EAST);
attributes.add(humanAtt, BorderLayout.SOUTH);
JPanel masterPanel = new JPanel();
masterPanel.setLayout(new GridLayout(6, 1));
masterPanel.add(new JLabel("Pick your race"));
masterPanel.add(charRace);
masterPanel.add(new JLabel("Pick your Class"));
masterPanel.add(charClass);
masterPanel.add(new JLabel("Customize your ability points"));
masterPanel.add(attributes);
STRplus.addActionListener((ActionEvent e) -> {
attPts[0] = subPool(attPts[0], attPts[1]);
attPts[1] = increaseAtt(attPts[1]);
});
STRminus.addActionListener((ActionEvent e) -> {
attPts[0] = addPool(attPts[0], attPts[1]);
attPts[1] = decreaseAtt(attPts[1]);
});
CONplus.addActionListener((ActionEvent e) -> {
attPts[0] = subPool(attPts[0], attPts[2]);
attPts[2] = increaseAtt(attPts[2]);
});
CONminus.addActionListener((ActionEvent e) -> {
attPts[0] = addPool(attPts[0], attPts[2]);
attPts[2] = decreaseAtt(attPts[2]);
});
DEXplus.addActionListener((ActionEvent e) -> {
attPts[0] = subPool(attPts[0], attPts[3]);
attPts[3] = increaseAtt(attPts[3]);
});
DEXminus.addActionListener((ActionEvent e) -> {
attPts[0] = addPool(attPts[0], attPts[3]);
attPts[3] = decreaseAtt(attPts[3]);
});
INTELplus.addActionListener((ActionEvent e) -> {
attPts[0] = subPool(attPts[0], attPts[4]);
attPts[4] = increaseAtt(attPts[4]);
});
INTELminus.addActionListener((ActionEvent e) -> {
attPts[0] = addPool(attPts[0], attPts[4]);
attPts[4] = decreaseAtt(attPts[4]);
});
charRace.addItemListener((ItemEvent e) -> {
int[] attPts1 = {32, 8, 8, 8, 8, 8, 8};
if ((charRace.getSelectedIndex()) == 0) {
humanAtt.setVisible(false);
attPts1[1] = (attPts1[1] + 2);
}
if (((charRace.getSelectedIndex()) == 1) || ((charRace.getSelectedIndex()) == 4)) {
humanAtt.setVisible(false);
attPts1[2] = (attPts1[2] + 2);
}
});
}
public int subPool(int pool, int att) {
if (att == 18)
pool = pool - 0;
else {
if (att == 17)
pool = pool - 4;
else {
if (att == 16)
pool = pool - 3;
else {
if (att > 12)
pool = pool - 2;
else
pool = pool - 1;
}
}
}
return pool;
}
public int addPool(int pool, int att) {
if (att == 18)
pool = pool + 4;
else {
if (att == 17)
pool = pool + 3;
else {
if (att > 13)
pool = pool + 2;
else {
if (att > 8)
pool = pool - 2;
else
pool = pool - 0;
}
}
}
return pool;
}
public int increaseAtt(int att) {
if (att < 18)
att++;
return att;
}
public int decreaseAtt(int att) {
if (att > 8)
att = att - 1;
return att;
}
}
它给了我是
Main.java:18: error: illegal start of expression
public final String[] classes = {"Mage", "Warrior", "Ranger"};
^
Main.java:19: error: illegal start of expression
public final JComboBox<String>charClass = new JComboBox<String>(classes);
^
Main.java:25: error: illegal start of expression
public final JButton STRplus = new JButton("+");
^
Main.java:26: error: illegal start of expression
public final JButton STRminus = new JButton("-");
^
Main.java:28: error: illegal start of expression
public final JButton CONplus = new JButton("+");
^
Main.java:29: error: illegal start of expression
public final JButton CONminus = new JButton("-");
^
Main.java:31: error: illegal start of expression
public final JButton DEXplus = new JButton("+");
^
Main.java:32: error: illegal start of expression
public final JButton DEXminus = new JButton("-");
^
Main.java:34: error: illegal start of expression
public final JButton INTELplus = new JButton("+");
^
Main.java:35: error: illegal start of expression
public final JButton INTELminus = new JButton("-");
^
Main.java:38: error: illegal start of expression
public final String[] bonusAtt = {"Strength", "Constitution", "Dexterity",
^
Main.java:40: error: illegal start of expression
public JComboBox<String>humanAtt = new JComboBox<String>(bonusAtt);
如果你能在正确的方向指向我,那将是极大的赞赏
你试图声明一个方法内部成员字段中的错误。你不能那样做。
public static void main(String[]args)
{
public final String[] classes = {"Mage", "Warrior", "Ranger"};
...
变量应该是成员变量,如果它们是该类的一个实例的属性,可由该类的所有成员访问。如果仅在一次调用某个方法时使用它们,它们应该是局部变量。
如果你打算声明局部变量,他们不需要访问控制。局部变量只能从方法定义中访问。删除public
。
public static void main(String[]args)
{
final String[] classes = {"Mage", "Warrior", "Ranger"};
...
如果你打算声明成员变量,他们不能在main()
方法内部声明。将它们移出。
final String[] classes = {"Mage", "Warrior", "Ranger"};
...
public static void main(String[]args)
{
...
我最初根本没有main()方法,我试图在网上查找它,并且说它添加它并且程序应该运行良好。有我应该去的地方吗? – xAnghellic
您可以按照成员数据和方法的顺序将'main()'放在任意位置。但是你需要1)决定你想要它做什么,2)将成员声明移到它之外,3)从局部变量中删除'public'。通常,main()方法将创建该类的一个实例(例如'new CharacterCreator()'),并调用它的方法。 –
你不能公开声明的方法:
public final String[] classes = {"Mage", "Warrior", "Ranger"};
public final JComboBox<String>charClass = new JComboBox<String>(classes);
public final JButton STRplus = new JButton("+");
public final JButton STRminus = new JButton("-");
public final JButton CONplus = new JButton("+");
public final JButton CONminus = new JButton("-");
public final JButton DEXplus = new JButton("+");
public final JButton DEXminus = new JButton("-");
public final JButton INTELplus = new JButton("+");
public final JButton INTELminus = new JButton("-");
public final String[] bonusAtt = {"Strength", "Constitution", "Dexterity",
"Intelligence"};
public JComboBox<String>humanAtt = new JComboBox<String>(bonusAtt);
将其更改为:
String[] classes = {"Mage", "Warrior", "Ranger"};
JComboBox<String>charClass = new JComboBox<String>(classes);
JButton STRplus = new JButton("+");
JButton STRminus = new JButton("-");
JButton CONplus = new JButton("+");
JButton CONminus = new JButton("-");
JButton DEXplus = new JButton("+");
JButton DEXminus = new JButton("-");
JButton INTELplus = new JButton("+");
JButton INTELminus = new JButton("-");
String[] bonusAtt = {"Strength", "Constitution", "Dexterity",
"Intelligence"};
JComboBox<String>humanAtt = new JComboBox<String>(bonusAtt);
这应该可以解决的编译错误。如果你想让它们成为类变量,那么将它们移到main之外,这也应该修复你的错误。
那么将该方法与我拥有的方法放在一起的理想地点在哪里?将取出这2个修复所有的错误即将获得? – xAnghellic
为你的情况使用我已经提供的解决方案,因为你不需要然后成为课堂级别。 – StackFlowed
你不能有一个方法内访问修饰符:
下面,从您的代码段,是错误的:
public static void main(String[]args)
{
public final String[] classes = {"Mage", "Warrior", "Ranger"};
解决方案:
- 采取这些东西了的
main
方法。 - 删除访问修饰符(在你的情况下,删除
public
)
哪里可以把我的方法放在最好的地方? – xAnghellic
对于像'charClass','STRplus'等所有变量,您在错误中看到的只是将它们全部放在'main'之前。 – Abubakkar
的访问说明符的方法中都没有使用,那是你的'主()'方法在做什么。 – YoungHobbit
'公共静态无效main()'是一种方法,这意味着访问说明符是不必要的和非法的,例如方法启动前的'public static String'和方法内部的'String'。 –