Java interview questions - write the results to see the program switch

See the results of written procedures

class SwitchTest {
	public static void main(String[] args) {
		int x = 2;
		int y = 3;
		switch(x){
			default:
				y++;
				break;
			case 3:
				y++;
			case 4:
				y++;
		}
		System.out.println("y="+y);//4
		System.out.println("---------------");
		
		int a = 2;
		int b = 3;
		switch(a){
			default: //a=2 走的是default,在case穿透2次b=6;
				b++;
			case 3:
				b++;
			case 4:
				b++;
		}
		System.out.println("b="+b);//6
	}
}

result:

y=4 
---------------
b=6 
Published 117 original articles · won praise 0 · Views 1057

Guess you like

Origin blog.csdn.net/qq_40332952/article/details/104658324