I'm trying to change this code to a for loop, but i have some problems
panel[1].setBackground(Color.red);
panel[2].setBackground(Color.white);
panel[3].setBackground(Color.red);
panel[4].setBackground(Color.white);
panel[5].setBackground(Color.red);
panel[6].setBackground(Color.white);
panel[7].setBackground(Color.red);
panel[8].setBackground(Color.white);
panel[9].setBackground(Color.red);
panel[10].setBackground(Color.white);
new code - for
for (int i = 0; i < panel.length; i++) {
panel[(i*2)+1].setBackground(Color.red);//i think that is correct, or no?
panel[(i*3)+1].setBackground(Color.white); //problem here
}
thanks
(i*2)+1and(i*3)+1as your loop progresses. One the first iteration,i = 0, so they will be(0*2)+1 = 1and(0*3)+1 = 1, so we're already off to a bad start.