2015-11-04 96 views
1

有人可以幫我調試這個請。我試圖回到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); 

如果你能在正確的方向指向我,那將是極大的讚賞

+1

的訪問說明符的方法中都沒有使用,那是你的'主()'方法在做什麼。 – YoungHobbit

+0

'公共靜態無效main()'是一種方法,這意味着訪問說明符是不必要的和非法的,例如方法啓動前的'public static String'和方法內部的'String'。 –

回答

2

你試圖聲明一個方法內部成員字段中的錯誤。你不能那樣做。

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) 
{ 
    ... 
+0

我最初根本沒有main()方法,我試圖在網上查找它,並且說它添加它並且程序應該運行良好。有我應該去的地方嗎? – xAnghellic

+0

您可以按照成員數據和方法的順序將'main()'放在任意位置。但是你需要1)決定你想要它做什麼,2)將成員聲明移到它之外,3)從局部變量中刪除'public'。通常,main()方法將創建該類的一個實例(例如'new CharacterCreator()'),並調用它的方法。 –

1

你不能公開聲明的方法:

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之外,這也應該修復你的錯誤。

+0

那麼將該方法與我擁有的方法放在一起的理想地點在哪裏?將取出這2個修復所有的錯誤即將獲得? – xAnghellic

+0

爲你的情況使用我已經提供的解決方案,因爲你不需要然後成爲課堂級別。 – StackFlowed

2

你不能有一個方法內訪問修飾符:

下面,從您的代碼段,是錯誤的:

public static void main(String[]args) 
{ 

    public final String[] classes = {"Mage", "Warrior", "Ranger"}; 

解決方案:

  1. 採取這些東西了的main方法。
  2. 刪除訪問修飾符(在你的情況下,刪除public
+0

哪裏可以把我的方法放在最好的地方? – xAnghellic

+0

對於像'charClass','STRplus'等所有變量,您在錯誤中看到的只是將它們全部放在'main'之前。 – Abubakkar