如何在Java中使用嵌套for循环创建方形10x10网格?

如何在Java中使用嵌套for循环创建方形10x10网格?,java,Java,我正在尝试使用Java中的for循环创建一个10x10网格。我可以创建上下移动但不重复的行 for(int i = 1; i < temperatures.length; i++) { temperatures[i] = (temperatures[i-1] + 12) / 2; //takes average of 12 and previous temp } } publi

我正在尝试使用Java中的for循环创建一个10x10网格。我可以创建上下移动但不重复的行

            for(int i = 1; i < temperatures.length; i++) {
                temperatures[i] = (temperatures[i-1] + 12) / 2; //takes average of 12 and previous temp
            }
        }

        public void paint(Graphics g) {
            for(int y = 1; y < 9; y++) {
                g.setColor(Color.black);
                g.drawRect(10, 10, 10, 10);
                g.drawRect(10, 10, 10, 20);
                g.drawRect(10, 10, 10, 30);
                g.drawRect(10, 10, 10, 40);
                g.drawRect(10, 10, 10, 50);
                g.drawRect(10, 10, 10, 60);
                g.drawRect(10, 10, 10, 70);
                g.drawRect(10, 10, 10, 80);
                g.drawRect(10, 10, 10, 90);
                g.drawRect(10, 10, 10, 100);

                for(int x = 1; x < 9; x++) {
                g.setColor(Color.black);
                g.drawRect(10, 10, 10, 10);
                g.drawRect(10, 10, 20, 10);
                g.drawRect(10, 10, 30, 10);
                g.drawRect(10, 10, 40, 10);
                g.drawRect(10, 10, 50, 10);
                g.drawRect(10, 10, 60, 10);
                g.drawRect(10, 10, 70, 10);
                g.drawRect(10, 10, 80, 10);
                g.drawRect(10, 10, 90, 10);
                g.drawRect(10, 10, 100, 10);
            }
        } 
    }
}
for(int i=1;i
宽度=10;
高度=10;

对于(x=0;x我对上一个答案的2美分:

要确保栅格填充整个区域,请执行以下操作:

int width = totalWidth / 10;
int height= totalHeight / 10;
for(int row=0;row<10;row++){
    for(int col=0;col<10;col++){
        g.drawRect(row*width,col*height,width,height);
    }
}
int-width=totalWidth/10;
内部高度=总高度/10;

对于(int row=0;row)我不想做网格,但我现在就做。谢谢:考虑到问题的简单性质,我想在你的答案中也定义宽度和高度。记住正确缩进,这将有助于更好地理解你的代码
int width = totalWidth / 10;
int height= totalHeight / 10;
for(int row=0;row<10;row++){
    for(int col=0;col<10;col++){
        g.drawRect(row*width,col*height,width,height);
    }
}