我目前仍在学习java,所以这可能只是一个业余错误
目的是“for”循环会在给定不同高度的条形图上绘制出一个“条形”数组(为糟糕的格式道歉)
static int[]cwMarks = new int[4];
static int[]examMarks = new int[4];
static int barWidth = 4; //Standard bar width
private static int posX = -15; //Decides X position of bar
private static int posY= 210; //Decides Y position of bar
static int barCount = 8; //Tracks number of bars needing to be drawn
static Bar[] barArr = new Bar[barCount]; //Array storing Bars
cwMarks[0]=50;
cwMarks[1]=100;
cwMarks[2]=20;
cwMarks[3]=100;
examMarks[0]=50;
examMarks[1]=100;
examMarks[2]= 20;
examMarks[3]= 30;
draw();
public static void draw()
{
for (int i=0,j=0;i<barCount;j++) //i represents the element of barArr, j represents element of cwMarks/examMarks
{
barArr[i].moveHorizontal(posX);
barArr[i].changeSize(barWidth, cwMarks[j]);
barArr[i].moveVertical(posY-cwMarks[j]);
barArr[i].makeVisible();
i++;
barArr[i].moveHorizontal(posX+barWidth+1);
barArr[i].changeSize(barWidth, examMarks[j]);
barArr[i].moveVertical(posY-examMarks[j]);
barArr[i].makeVisible();
i++;
posX=+5;
}
}
`
我假设这将创建 4 对 2 条,但循环似乎只执行它的代码两次(产生 2 对 2)。这是绘制的:
关于为什么会这样的任何想法?