Switch executes all case statements
Can someone explain me why my loop is going from 0 to 7? I need to get only in the first 3 cases, but the program is going through all.. The code is in the show() method.
for(brojac=0; brojac<3; brojac++){
switch(brojac){
case 1:
figura1.setPosition(pomx[random], pomy[random]);
stage.addActor(figura1);
System.out.println("1");
case 2:
figura2.setPosition(pomx[random], pomy[random]);
stage.addActor(figura2);
System.out.println("2");
case 3:
figura3.setPosition(pomx[random], pomy[random]);
stage.addActor(figura3);
System.out.println("3");
case 4:
figura4.setPosition(pomx[random], pomy[random]);
stage.addActor(figura4);
System.out.println("4");
case 5:
figura5.setPosition(pomx[random], pomy[random]);
stage.addActor(figura5);
System.out.println("5");
case 6:
figura6.setPosition(pomx[random], pomy[random]);
stage.addActor(figura6);
System.out.println("6");
case 7:
figura7.setPosition(pomx[random], pomy[random]);
stage.addActor(figura7);
System.out.println("7");
}
}
Solution 1:
Because the break is missing. After every case you need the break keyword. For a detailed explanation see switch statement java tutorial
e.g.
for(brojac=0; brojac<3; brojac++){
switch(brojac){
case 1:
figura1.setPosition(pomx[random], pomy[random]);
stage.addActor(figura1);
System.out.println("1");
break; /// Break here
case 2:
figura2.setPosition(pomx[random], pomy[random]);
stage.addActor(figura2);
System.out.println("2");
break; /// Break here
case 3:
figura3.setPosition(pomx[random], pomy[random]);
stage.addActor(figura3);
System.out.println("3");
break; /// Break here
case 4:
figura4.setPosition(pomx[random], pomy[random]);
stage.addActor(figura4);
System.out.println("4");
break; /// Break here
case 5:
figura5.setPosition(pomx[random], pomy[random]);
stage.addActor(figura5);
System.out.println("5");
break; /// Break here
case 6:
figura6.setPosition(pomx[random], pomy[random]);
stage.addActor(figura6);
System.out.println("6");
break; /// Break here
case 7:
figura7.setPosition(pomx[random], pomy[random]);
stage.addActor(figura7);
System.out.println("7");
break; /// Break here
}
}
Otherwise the other cases will be executed if one of the above cases gets evaluated to true.
Solution 2:
That's how Java's switch
statements work.
They "fall through" the cases once one of them matches. You need to add break;
after all your cases.
for(brojac=0; brojac<3; brojac++){
switch(brojac){
case 1:
figura1.setPosition(pomx[random], pomy[random]);
stage.addActor(figura1);
System.out.println("1");
break;
case 2:
figura2.setPosition(pomx[random], pomy[random]);
stage.addActor(figura2);
System.out.println("2");
break;
case 3:
figura3.setPosition(pomx[random], pomy[random]);
stage.addActor(figura3);
System.out.println("3");
break;
case 4:
figura4.setPosition(pomx[random], pomy[random]);
stage.addActor(figura4);
System.out.println("4");
break;
case 5:
figura5.setPosition(pomx[random], pomy[random]);
stage.addActor(figura5);
System.out.println("5");
break;
case 6:
figura6.setPosition(pomx[random], pomy[random]);
stage.addActor(figura6);
System.out.println("6");
break;
case 7:
figura7.setPosition(pomx[random], pomy[random]);
stage.addActor(figura7);
System.out.println("7");
break;
}
}