Java Netbeans For循环错误:找不到符号类,<identifier>预期?

时间:2015-06-11 21:42:19

标签: java for-loop netbeans

我正在制作一个刽子手游戏,这是我目前的代码:

//Declaring the array
    String [] WordList = {"anonymous", "tolerate", "appreciation", "commissioner", "harm", "flexibility", "instructional", "scramble", "casino", "tumor"};

// Getting a random word for the game
int num = ((int)Math.ceil(Math.random()*(WordList.length)));
String word = WordList[num];

// The puzzle to be displayed for the user to guess
int letters = word.length();
String hiddenWord = "";
for (int i=0; i<letters; i++){
    hiddenWord+="-";
}

在for循环中,我收到错误提示&#39;非法启动类型,找不到符号:class i&#39;。 &#39;我&#39;应该是一个整数,我已经宣布了,所以我不确定为什么它要求上课。 还有另一个错误说,&#39;标识符预期&#39;并且那里缺少&gt;。作为我的&lt;用于比较(而不是作为声明或标识符),我不理解这个错误。

为了澄清,我应该补充一点,这是netbeans中的桌面应用程序。代码位于以下行之后:

public class HangmanGameView extends FrameView {

我的大部分代码都位于我的按钮的actionPerformed代码中。我需要能够从按钮的代码中访问word和hiddenWord变量,这就是我将它们放在这里的原因。如果那是问题,我不肯定。我也不确定在哪里放置代码;据我所知,没有主要方法。

编辑:我没有发布完整的代码,因为这是我现在所做的一切;其余的都是由NetBeans自动生成的,我认为它不相关。但这是:

/*
 * HangmanGameView.java
 */

package hangmangame;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class HangmanGameView extends FrameView {

//Declaring the array
String [] WordList = {"anonymous", "tolerate", "appreciation", "commissioner", "harm", "flexibility", "instructional", "scramble", "casino", "tumor"};

// Getting a random word for the game
int num = ((int)Math.ceil(Math.random()*(WordList.length)));
String word = WordList[num];

// The puzzle to be displayed for the user to guess
int letters = word.length();
String hiddenWord = "";
for (int i=0; i<letters; i++){
    hiddenWord+="-";
}


public HangmanGameView(SingleFrameApplication app) {

    super(app);

    initComponents();

    // status bar initialization - message timeout, idle icon and busy animation, etc
    ResourceMap resourceMap = getResourceMap();
    int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
    messageTimer = new Timer(messageTimeout, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            statusMessageLabel.setText("");
        }
    });
    messageTimer.setRepeats(false);
    int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
    for (int i = 0; i < busyIcons.length; i++) {
        busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
    }
    busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
            statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
        }
    });
    idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);

    // connecting action tasks to status bar via TaskMonitor
    TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
    taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
        public void propertyChange(java.beans.PropertyChangeEvent evt) {
            String propertyName = evt.getPropertyName();
            if ("started".equals(propertyName)) {
                if (!busyIconTimer.isRunning()) {
                    statusAnimationLabel.setIcon(busyIcons[0]);
                    busyIconIndex = 0;
                    busyIconTimer.start();
                }
                progressBar.setVisible(true);
                progressBar.setIndeterminate(true);
            } else if ("done".equals(propertyName)) {
                busyIconTimer.stop();
                statusAnimationLabel.setIcon(idleIcon);
                progressBar.setVisible(false);
                progressBar.setValue(0);
            } else if ("message".equals(propertyName)) {
                String text = (String)(evt.getNewValue());
                statusMessageLabel.setText((text == null) ? "" : text);
                messageTimer.restart();
            } else if ("progress".equals(propertyName)) {
                int value = (Integer)(evt.getNewValue());
                progressBar.setVisible(true);
                progressBar.setIndeterminate(false);
                progressBar.setValue(value);
            }
        }
    });
}

@Action
public void showAboutBox() {
    if (aboutBox == null) {
        JFrame mainFrame = HangmanGameApp.getApplication().getMainFrame();
        aboutBox = new HangmanGameAboutBox(mainFrame);
        aboutBox.setLocationRelativeTo(mainFrame);
    }
    HangmanGameApp.getApplication().show(aboutBox);
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    mainPanel = new javax.swing.JPanel();
    titleLabel = new javax.swing.JLabel();
    startButton = new javax.swing.JButton();
    guesswordOutput = new javax.swing.JLabel();
    guessLabel = new javax.swing.JLabel();
    guessInput = new javax.swing.JTextField();
    guessButton = new javax.swing.JButton();
    picLabel = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    gameOutput = new javax.swing.JTextArea();
    menuBar = new javax.swing.JMenuBar();
    javax.swing.JMenu fileMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
    javax.swing.JMenu helpMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
    statusPanel = new javax.swing.JPanel();
    javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
    statusMessageLabel = new javax.swing.JLabel();
    statusAnimationLabel = new javax.swing.JLabel();
    progressBar = new javax.swing.JProgressBar();

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(hangmangame.HangmanGameApp.class).getContext().getResourceMap(HangmanGameView.class);
    mainPanel.setBackground(resourceMap.getColor("mainPanel.background")); // NOI18N
    mainPanel.setName("mainPanel"); // NOI18N

    titleLabel.setFont(titleLabel.getFont().deriveFont(titleLabel.getFont().getSize()+19f));
    titleLabel.setForeground(resourceMap.getColor("titleLabel.foreground")); // NOI18N
    titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    titleLabel.setText(resourceMap.getString("titleLabel.text")); // NOI18N
    titleLabel.setName("titleLabel"); // NOI18N

    startButton.setFont(startButton.getFont().deriveFont(startButton.getFont().getStyle() & ~java.awt.Font.BOLD, startButton.getFont().getSize()+5));
    startButton.setForeground(resourceMap.getColor("startButton.foreground")); // NOI18N
    startButton.setText(resourceMap.getString("startButton.text")); // NOI18N
    startButton.setToolTipText(resourceMap.getString("startButton.toolTipText")); // NOI18N
    startButton.setName("startButton"); // NOI18N
    startButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            startButtonActionPerformed(evt);
        }
    });

    guesswordOutput.setText(resourceMap.getString("guesswordOutput.text")); // NOI18N
    guesswordOutput.setName("guesswordOutput"); // NOI18N

    guessLabel.setFont(resourceMap.getFont("guessLabel.font")); // NOI18N
    guessLabel.setText(resourceMap.getString("guessLabel.text")); // NOI18N
    guessLabel.setName("guessLabel"); // NOI18N

    guessInput.setBackground(resourceMap.getColor("guessInput.background")); // NOI18N
    guessInput.setFont(resourceMap.getFont("guessInput.font")); // NOI18N
    guessInput.setText(resourceMap.getString("guessInput.text")); // NOI18N
    guessInput.setToolTipText(resourceMap.getString("guessInput.toolTipText")); // NOI18N
    guessInput.setName("guessInput"); // NOI18N

    guessButton.setFont(resourceMap.getFont("guessButton.font")); // NOI18N
    guessButton.setForeground(resourceMap.getColor("guessButton.foreground")); // NOI18N
    guessButton.setText(resourceMap.getString("guessButton.text")); // NOI18N
    guessButton.setName("guessButton"); // NOI18N
    guessButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            guessButtonActionPerformed(evt);
        }
    });

    picLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    picLabel.setIcon(resourceMap.getIcon("picLabel.icon")); // NOI18N
    picLabel.setText(resourceMap.getString("picLabel.text")); // NOI18N
    picLabel.setName("picLabel"); // NOI18N

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    gameOutput.setBackground(resourceMap.getColor("gameOutput.background")); // NOI18N
    gameOutput.setColumns(20);
    gameOutput.setRows(5);
    gameOutput.setName("gameOutput"); // NOI18N
    jScrollPane1.setViewportView(gameOutput);

    javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
    mainPanel.setLayout(mainPanelLayout);
    mainPanelLayout.setHorizontalGroup(
        mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
        .addGroup(mainPanelLayout.createSequentialGroup()
            .addGap(42, 42, 42)
            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(picLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addComponent(guessLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(guessInput, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(46, 46, 46)
                    .addComponent(guessButton)))
            .addContainerGap(55, Short.MAX_VALUE))
        .addGroup(mainPanelLayout.createSequentialGroup()
            .addGap(181, 181, 181)
            .addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(189, Short.MAX_VALUE))
        .addComponent(guesswordOutput, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
    );
    mainPanelLayout.setVerticalGroup(
        mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(mainPanelLayout.createSequentialGroup()
            .addGap(21, 21, 21)
            .addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(26, 26, 26)
            .addComponent(guesswordOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(38, 38, 38)
            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(guessLabel)
                .addComponent(guessButton)
                .addComponent(guessInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(jScrollPane1)
                .addComponent(picLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
            .addGap(39, 39, 39))
    );

    menuBar.setName("menuBar"); // NOI18N

    fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
    fileMenu.setName("fileMenu"); // NOI18N

    javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(hangmangame.HangmanGameApp.class).getContext().getActionMap(HangmanGameView.class, this);
    exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
    exitMenuItem.setName("exitMenuItem"); // NOI18N
    fileMenu.add(exitMenuItem);

    menuBar.add(fileMenu);

    helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
    helpMenu.setName("helpMenu"); // NOI18N

    aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
    aboutMenuItem.setName("aboutMenuItem"); // NOI18N
    helpMenu.add(aboutMenuItem);

    menuBar.add(helpMenu);

    statusPanel.setName("statusPanel"); // NOI18N

    statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

    statusMessageLabel.setName("statusMessageLabel"); // NOI18N

    statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

    progressBar.setName("progressBar"); // NOI18N

    javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
    statusPanel.setLayout(statusPanelLayout);
    statusPanelLayout.setHorizontalGroup(
        statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
        .addGroup(statusPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(statusMessageLabel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 315, Short.MAX_VALUE)
            .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(statusAnimationLabel)
            .addContainerGap())
    );
    statusPanelLayout.setVerticalGroup(
        statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(statusPanelLayout.createSequentialGroup()
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(statusMessageLabel)
                .addComponent(statusAnimationLabel)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(3, 3, 3))
    );

    setComponent(mainPanel);
    setMenuBar(menuBar);
    setStatusBar(statusPanel);
}// </editor-fold>

private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // Initializes the game and displays the empty word for the player

}

private void guessButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
}

// Variables declaration - do not modify
private javax.swing.JTextArea gameOutput;
private javax.swing.JButton guessButton;
private javax.swing.JTextField guessInput;
private javax.swing.JLabel guessLabel;
private javax.swing.JLabel guesswordOutput;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JLabel picLabel;
private javax.swing.JProgressBar progressBar;
private javax.swing.JButton startButton;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
private javax.swing.JLabel titleLabel;
// End of variables declaration

private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;

private JDialog aboutBox;
}

最终编辑: 我将我的for循环移动到开始按钮代码内部 - 在开头留下hiddenWord字符串的初始声明 - 现在它正常工作。谢谢你的帮助!

0 个答案:

没有答案