我正在尝试使用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);
}
}
}
}
答案 0 :(得分:9)
width=10;
height=10;
for(x=0;x<10;x++)
{
for(y=0;y<10;y++)
{
g.drawRect(x*width,y*height,width,height);
}
}
答案 1 :(得分:2)
我对前一个答案的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);
}
}