Dont understand Output. Could someone explain?


#1

I know the output of this is 15.0 the question is WHY?

public class Cases{
public static void main (String[] arguments){
float x = 9;
float y = 5;

    int z = (int)(x/y);
    switch(z) {
        case 1:
            x=x+2;
        case 2:
            x=x+3;
        default:
            x=x+1;
    }
    System.out.println("Value of x: " + x);
}

}


#2

Switch cases fall through to the next one unless you break
Or at least that’s what I assume you’re missing, you haven’t said what you think should happen instead


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.