“非法表达”

时间:2015-11-04 18:37:25

标签: java

请有人帮我调试一下。我正试图回到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);

如果你能指出我正确的方向,我们将不胜感激

3 个答案:

答案 0 :(得分: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)
{
   ...

答案 1 :(得分:2)

您无法在方法中使用访问修饰符:

下面,代码中的代码段是错误的:

$mail = new PHPMailer();

$mail->SMTPDebug = false;                               

$mail->isSMTP();                                      
$mail->Host = 'smtp.gmail.com';  
$mail->SMTPAuth = true;                               
$mail->Username = 'xyz@gmail.com';                 
$mail->Password = 'pass';                           
$mail->SMTPSecure = 'tls';                            
$mail->Port = 587;                                    


$mail->setFrom('xyz@gmail.com', 'Name');
$mail->addAddress('xyz@gmail.com');               
$mail->addReplyTo('xyz@gmail.com', 'Information');
$mail->isHTML(true);                                  

$mail->Subject = 'subject';
$mail->Body    = 'Body';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

解决方案:

  1. public static void main(String[]args) { public final String[] classes = {"Mage", "Warrior", "Ranger"}; 方法中取出这些东西。
  2. 删除访问修饰符(在您的情况下,删除main

答案 2 :(得分:1)

您不能在方法中使用公开声明:

column_one, column_2

将其更改为:

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);

这应该可以解决编译器错误。如果你想让它们成为类变量,那么将它们移到main之外,这也应该修复你的错误。