Java条件语句-if...else
- if...else语句
- if...else if...else语句
- 嵌套的if...else语句
Java条件语句-switch case
直接上代码:
package pkg2020华南虎; /** * * @author yl */ public class TestSwitch { public static void main(String[] args) { char grade='C'; switch(grade){ case 'A': System.out.println("Perfect"); break; case 'B': System.out.println("normal"); break; case 'C': System.out.println("OK"); break; default: System.out.println("Unknow mistake!"); } System.out.println("你的等级是:"+grade); } }
如果case语句块中没有break语句时,JVM并不会顺序输出每一个case对应的返回值,而是继续匹配,匹配不成功,则返回default。
package pkg2020华南虎; /** * * @author yl */ public class TestSwitch02 { public static void main(String[] args) { int i=1; switch(i){ case 0: System.out.println("0"); case 1: System.out.println("1"); case 2: System.out.println("2"); default: System.out.println("default"); } } }
输出结果:
1
2
default
package pkg2020华南虎; /** * * @author yl */ public class TestSwitch02 { public static void main(String[] args) { int i=1; switch(i){ case 0: System.out.println("0"); case 1: System.out.println("1"); case 2: System.out.println("2"); break; default: System.out.println("default"); } } }
输出结果:
1
2