Text changes on the label with switch

Asked

Viewed 69 times

0

You know that initial Pokemon conversation?

"Welcome to the world Pokemon" - ENTER
"Are you a girl or a boy?" -Enter
CONTINUES....

So, I’m trying to do a Java swing, at the moment it’s like this:

private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        jLabel1.setText(mudarTexto());
        //i++;
    }                                    


    public String mudarTexto(){
            //jTexto.setText("Olá,bem vindo ao mundo pokémon");
            int i = 0;
            switch(i){
                case 0:
                    c ="oi";
                case 1:
                    c  =  "Olá,bem vindo ao mundo pokémon";
                case 2:
                    c = "Você é garoto ou garota?";
                case 3:
                    c = "aa";
                default:
                    c = "c";

            }

            return c ;
    }

But the switch don’t read mine i as zero , it only prints the last instruction of the switch that in the case is default, if the last one was case 3, then it would be printable, how can I solve this problem?

1 answer

2


This happens because you are not leaving the switch upon finding the case correct, so it goes through all. Change by adding the "stops" between cases:

switch(i){
    case 0:
        c ="oi";
        break;
    case 1:
        c  =  "Olá,bem vindo ao mundo pokémon";
        break;
    case 2:
        c = "Você é garoto ou garota?";
        break;
    case 3:
        c = "aa";
        break;
    default:
        c = "c";
}

Note that in the default has not been added, because there will already come out of the switch anyway.

  • po, vdd, vlw there

  • @Dr.G good that helped. You can also mark the answer as accepted, to serve as reference for other people :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.