Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

java - 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");

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...