Java:绘制乘法表的起始循环增量
我无法确定如何删除此表中的0。我曾尝试在网上查找它,但用这种方法(可能没有正确搜索)几乎没有成功。除了一些风格上的改变外,我还试图让图1看起来像图2 我非常感谢你的帮助 代码:()使用的绘图面板Java:绘制乘法表的起始循环增量,java,for-loop,drawing,increment,multiplication,Java,For Loop,Drawing,Increment,Multiplication,我无法确定如何删除此表中的0。我曾尝试在网上查找它,但用这种方法(可能没有正确搜索)几乎没有成功。除了一些风格上的改变外,我还试图让图1看起来像图2 我非常感谢你的帮助 代码:()使用的绘图面板 import java.awt.*; public class IfGridFor { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(400, 520);
import java.awt.*;
public class IfGridFor {
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(400, 520);
panel.setBackground(Color.blue);
Graphics g = panel.getGraphics();
int sizeX = 40;
int sizeY = 40;
for (int x = 0; x < 10; x++) {
for (int y = 0; y <= 12; y++) {
int cornerX = x*sizeX;
int cornerY = y*sizeY;
if ((x + y) % 2 == 0)
g.setColor(Color.green);
else
g.setColor(Color.yellow);
g.fillRect(cornerX+1, cornerY+1, sizeX-2, sizeY-2);
g.setColor(Color.black);
g.drawString(x + " * " + y, cornerX + 5, cornerY + 15); // text is
g.drawString("= " + x * y, cornerX + 5, cornerY + 33); // offsets
}
}
}
}
import java.awt.*;
公共类IfGridFor{
公共静态void main(字符串[]args){
DrawingPanel=新的DrawingPanel(400520);
面板.立根背景(颜色.蓝色);
Graphics g=panel.getGraphics();
int-sizeX=40;
int-sizeY=40;
对于(int x=0;x<10;x++){
对于(int y=0;y您几乎完成了-您所需要的只是将显示内容从x
,y
,x*y
更改为(x+1)
,(y+1)
,(x+1)*(y+1)
,并将面板高度降低一行:
DrawingPanel panel = new DrawingPanel(400, 480); // 12 rows, not 13
...
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 12; y++) { // < instead of <=
...
g.drawString((x+1) + " * " + (y+1), cornerX + 5, cornerY + 15); // text is
g.drawString("" + (x+1) * (y+1), cornerX + 5, cornerY + 33); // offsets
}
}
DrawingPanel=newDrawingPanel(400480);//12行,不是13行
...
对于(int x=0;x<10;x++){
对于(int y=0;y<12;y++){//<而不是如果我正确理解了你的问题,你想删除最上面的一行和左边的一列吗?如果是的话,从1开始你的for
循环,而不是从零开始。另外,你的外循环应该有我尝试过的x的条件,即使在调整面板的时候,它们也没有蓝色空间。
int cornerX = x*sizeX;
int cornerY = y*sizeY;
int cornerX = (x-1)*sizeX;
int cornerY = (y-1)*sizeY;