for循环的类型非法开始? [英] Ilegal start of type for a for loop?

查看:67
本文介绍了for循环的类型非法开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会出现错误:java:16:错误:类型的非法启动for(int i = 0; i< 9; i ++){我检查了旧实验室,这是我认为的正确用法.

Why am I getting the error :java:16: error: illegal start of type for(int i = 0; i < 9; i++){ I have checked old labs and this is the correct usage of it I thought.

import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;


class Lab3Panel extends JPanel{

    Lab3Panel () {

        setLayout(new GridLayout(3,3));
    }


    Lab3Label label[] = new Lab3Label[9];

    for(int i = 0; i < 9; i++){

        label[i] = new Lab3Label();
        add(label[i]);
    }

推荐答案

for循环必须位于一对 {} 括号之间的方法,构造函数中或最低限度的位置.但是可能是构造函数.

The for loop needs to be in a method, a constructor, or at a bare minimum, between a pair of {} brackets. But probably a constructor.

这篇关于for循环的类型非法开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆