public class switcha { public static void main(String[] args ){ /*例子 int i = 123; switch (i) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); break; case 2: System.err.println("two"); break; default: System.out.println("default"); break; } */ /*输入季节,输出季节特色 String season = "abc"; switch(season){ case "spring": System.out.println("春暖花开"); break; case "summer": System.out.println("夏日炎炎"); break; case "autumn": System.out.println("秋高气爽"); break; case "winter": System.out.println("冬雪皑皑"); break; default: System.out.println("季节输入有误!,请重新输入:"); break; }*/ /*例题1、把小写类型的char型转为大写,只转换a,b,c,d,其他的输出“other” char i = 'f'; switch(i){ case 'a': System.out.println("A"); break; case 'b': System.out.println("B"); break; case 'c': System.out.println("C"); break; case 'd': System.out.println("D"); break; default: System.out.println("Other"); break; } */ /*例题2、对学生成绩大于60分的,输出“合格”;低于60分的,输出“不合格” int score = 70; int a = (score >= 60) ? 1:0; switch(a){ case 1 : System.out.println("合格"); break; case 0: System.out.println("不合格"); break; } */ /*例题3、根据指定的月份,打印该月份所属的季节:3,4,5-春天;6,7,8-夏天; * 9,10,11-秋季;12,1,2-冬季 int month = 5; switch(month){ case 3: case 4: case 5: System.out.println("春天"); break; case 6: case 7: case 8: System.err.println("夏天"); break; case 9: case 10: case 11: System.out.println("秋天"); break; case 12: case 1: case 2: System.out.println("冬天"); break; default: System.out.println("请输入正确的月份!"); break; } */ /**if 语句 化为 switch语句*/ //if语句 int a = 3;//初始化a的值 int x = 100; if(a==1){ x+=5; System.out.println(x); } else if(a==2){ x+=10; System.out.println(x); } else if(a==3){ x+=16; System.out.println(x); } else { x+=34; System.out.println(x); } //switch语句 int b =3;//初始化b的值 int y = 100; switch(b){ case 1: x += 5; System.out.println(y); break; case 2: x += 10; System.out.println(y); break; case 3: x += 16; System.out.println(y); break; default: x += 34; System.out.println(y); break; } } }
