java - Modification of value in switch case -
public class sample { public static void main(string[] args) { int = 9; switch (i) { default: system.out.println("default"); case 0: system.out.println("zero"); break; case 1: system.out.println("one"); case 2: system.out.println("two"); } } }
output:
default 0
although i
initialized 9, how possible case 0 picked? reason?
you miss break statement after first default case excution falls through second case.also put default case last excuted after other cases checked.
public static void main(string[] args) { int = 9; switch (i) { case 0: system.out.println("zero"); break; case 1: system.out.println("one"); break; case 2: system.out.println("two"); break; default: system.out.println("default"); break; } }
Comments
Post a Comment