Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java for循环获取错误“;“类型”的非法启动;_Java_Loops_For Loop - Fatal编程技术网

Java for循环获取错误“;“类型”的非法启动;

Java for循环获取错误“;“类型”的非法启动;,java,loops,for-loop,Java,Loops,For Loop,所以我尝试用java创建一个程序,它将创建一个10乘10的矩阵,每个元素随机显示1或0。以下是我到目前为止的情况: package random.matrix; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; class ex2 extends JFrame { class Random { GridLayout setLayout= new Grid

所以我尝试用java创建一个程序,它将创建一个10乘10的矩阵,每个元素随机显示1或0。以下是我到目前为止的情况:

package random.matrix;

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

class ex2 extends JFrame {

    class Random {
        GridLayout setLayout= new GridLayout(10, 10);

        for (int i = 0; i < 10; i++) {
            int number = (int) (Math.random() * 2);
            String str = Integer.toString(number);
            add(new JLabel(str, JLabel.CENTER));
        }
    }

    public static void main(String[] args) {
        JFrame frame = new ex2();
        frame.setTitle("RandomMatrix");
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
package random.matrix;
导入java.awt.GridLayout;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
类ex2扩展了JFrame{
类随机{
GridLayout setLayout=新的GridLayout(10,10);
对于(int i=0;i<10;i++){
整数=(int)(Math.random()*2);
字符串str=Integer.toString(数字);
添加(新JLabel(str,JLabel.CENTER));
}
}
公共静态void main(字符串[]args){
JFrame=newex2();
frame.setTitle(“随机矩阵”);
框架。设置尺寸(400400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

据我所知,这个程序应该运行得很好。然而,每次我尝试时,它都会说一些类似“非法启动类型”的话,特别是指for循环行。有人能帮我解决这个问题吗?我从未遇到过像这样的错误。

在类定义中不能有任意语句。也许你想把它放到构造函数里

class Random {
    public Random() {
        GridLayout setLayout = new GridLayout(10, 10);

        for (int i = 0; i < 10; i++)
        {
           int number = (int) (Math.random() * 2);
           String str = Integer.toString(number);
           setLayout.add(new JLabel(str, JLabel.CENTER));
        }
    }
}
类随机{
公共随机{
GridLayout setLayout=新的GridLayout(10,10);
对于(int i=0;i<10;i++)
{
整数=(int)(Math.random()*2);
字符串str=Integer.toString(数字);
添加(新的JLabel(str,JLabel.CENTER));
}
}
}

或者,您可以创建另一个方法并将其放在其中。

在类定义中不能有任意语句。也许你想把它放到构造函数里

class Random {
    public Random() {
        GridLayout setLayout = new GridLayout(10, 10);

        for (int i = 0; i < 10; i++)
        {
           int number = (int) (Math.random() * 2);
           String str = Integer.toString(number);
           setLayout.add(new JLabel(str, JLabel.CENTER));
        }
    }
}
类随机{
公共随机{
GridLayout setLayout=新的GridLayout(10,10);
对于(int i=0;i<10;i++)
{
整数=(int)(Math.random()*2);
字符串str=Integer.toString(数字);
添加(新的JLabel(str,JLabel.CENTER));
}
}
}

或者,您可以创建另一个方法并将其放在其中。

您需要将代码放在代码块中,例如方法或构造函数,而不是内部类的类块中

/**
 * TODO: Refactor later NOT to extend from JFrame
 */
class MyFrame extends JFrame {

    void initComponents() {
        GridLayout setLayout = new GridLayout(10, 10);

        for (int i = 0; i < 10; i++) {
          ...
        }
    }
    ...
}
/**
*TODO:稍后重构以不从JFrame扩展
*/
类MyFrame扩展了JFrame{
void initComponents(){
GridLayout setLayout=新的GridLayout(10,10);
对于(int i=0;i<10;i++){
...
}
}
...
}

您需要将代码放在代码块(如方法或构造函数)中,而不是放在内部类的类块中

/**
 * TODO: Refactor later NOT to extend from JFrame
 */
class MyFrame extends JFrame {

    void initComponents() {
        GridLayout setLayout = new GridLayout(10, 10);

        for (int i = 0; i < 10; i++) {
          ...
        }
    }
    ...
}
/**
*TODO:稍后重构以不从JFrame扩展
*/
类MyFrame扩展了JFrame{
void initComponents(){
GridLayout setLayout=新的GridLayout(10,10);
对于(int i=0;i<10;i++){
...
}
}
...
}
这个答案有用吗?这个答案有用吗?