Switch executes all case statements

前端 未结 2 1247
南方客
南方客 2020-11-29 11:50

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.

相关标签:
2条回答
  • 2020-11-29 12:40

    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;
    
        }
    }
    
    0 讨论(0)
  • 2020-11-29 12:44

    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.

    0 讨论(0)
提交回复
热议问题