switch(变量){
case 常量1:
语句1;
break;
case 常量2:
语句2;
break;
… …
case 常量N:
语句N;
break;
default:
语句;
break;
}
- public class SwitchTest {
- public static void main(String[] args) {
- /*
- switch (变量) { 变量的数据类型必须是byte,char,short,int, String, 枚举对象
- case 常量1 : // if (变量 == 常量1)
- 语句1;
- break; // 如果没有break语句, switch不是严格的分支
- case 常量2 : // else if (变量 == 常量2)
- 语句2;
- break;
- }
- */
- int a = 20;
- switch (a) {
- case 10 : // if (a == 10)
- System.out.println("a==10");
- break; // 中断, 打断, 破坏
- case 20 :
- System.out.println("a==20");
- break; // 中断, 打断, 破坏,
- //如果没有break, 一旦进入case,导致后面的所有case都能进入, 直到遇到break或switch结束
- case 30 :
- System.out.println("a==30");
- break; // 中断, 打断, 破坏
- default : // 缺省 相当于 else
- System.out.println("default");
- break; // 中断, 打断, 破坏
- }
- System.out.println("after switch");
- }
- }
- /*
- 使用switch语句改写下列if语句:
- int a = 3;
- int x = 100;
- if(a==1)
- x+=5;
- else if(a==2)
- x+=10;
- else if(a==3)
- x+=16;
- else
- x+=34;
- System.out.println(x);
- */
- class Exer4 {
- public static void main(String[] args) {
- int a = 3;
- int x = 100;
- switch (a) {
- case 1 :
- x += 5;
- break;
- case 2 :
- x += 10;
- break;
- case 3 :
- x += 16;
- break;
- default :
- x += 34;
- break;
- }
- System.out.println(x);
- }
- }
- class SwitchTest2 {
- //根据从命令行参数获取的月份,打印该月份所属的季节。
- //3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
- public static void main(String[] args) {
- int month = Integer.parseInt(args[0]);
- switch (month) {
- case 1 :
- case 2 :
- case 12 :
- System.out.println("冬季");
- break;
- case 3 :
- case 4 :
- case 5 :
- System.out.println("春季");
- break;
- case 6 :
- case 7 :
- case 8 :
- System.out.println("夏季");
- break;
- case 9 :
- case 10 :
- case 11 :
- System.out.println("秋季");
- break;
- default :
- System.out.println("月份错误");
- break;
- }
- }
- }
- /*
- 使用 switch 把小写类型的 char型转为大写。只转换 a, b, c, d, e. 其它的输出 “other”。
- 提示:接收命令行参数的字符方法
- char ch = args[0].charAt(0);
- */
- public class SwitchTest1 {
- public static void main(String[] args) {
- char ch = args[0].charAt(0);
- switch (ch) {
- 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;
- case 'e' :
- System.out.println("E");
- break;
- default :
- System.out.println("other");
- break;
- }
- }
- }
- class SwitchTest11 {
- public static void main(String[] args) {
- char ch = args[0].charAt(0);
- String str = "Other"; // 先假设一种输出内容
- switch (ch) {
- case 'a' :
- case 'b' :
- case 'c' :
- case 'd' :
- case 'e' :
- str = "" + (char)(ch - 32); // 如果假设不对, 则对之进行修改
- }
- System.out.println(str); // 统一输出即可
- }
- }
- //对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。
- public class SwitchTest2 {
- public static void main(String[] args) {
- int score = Integer.parseInt(args[0]);
- switch (score / 10) {
- case 0 :
- case 1 :
- case 2 :
- case 3 :
- case 4 :
- case 5 :
- System.out.println("不及格");
- break;
- case 6 :
- case 7 :
- case 8 :
- case 9 :
- System.out.println("及格");
- break;
- default :
- if (score == 100) {
- System.out.println("及格");
- break;
- }
- System.out.println("输入成绩非法");
- }
- }
- }
- /*
- 编写程序:从命令行参数中获取一个学生成绩,存放在变量score中,根据score的值输出
- 其对应的成绩等级:
- score>=90 等级:A
- 70=<score<90 等级: B
- 60=<score<70 等级: C
- score<60 等级:D
- int score = Integer.parseInt(args[0]);
- */
- public class SwitchTest3 {
- public static void main(String[] args) {
- int score = Integer.parseInt(args[0]);
- switch (score / 10) {
- case 9 :
- System.out.println("等级:A");
- break;
- case 8 :
- case 7 :
- System.out.println("等级:B");
- break;
- case 6 :
- System.out.println("等级:C");
- break;
- case 5 :
- case 4 :
- case 3 :
- case 2 :
- case 1 :
- case 0 :
- System.out.println("等级:D");
- break;
- default :
- if (score == 100){
- System.out.println("等级:A");
- break;
- }
- System.out.println("输入成绩非法");
- break;
- }
- }
- }
- /*
- 接收命令行参数年、月、日,判断这一天是当年的第几天
- 注:判断一年是否是闰年的标准:
- 1)可以被4整除,但不可被100整除
- 2)可以被400整除
- */
- public class SwitchTest4 {
- public static void main(String[] args) {
- int year = Integer.parseInt(args[0]);
- int month = Integer.parseInt(args[1]);
- int day = Integer.parseInt(args[2]);
- int sum = 0;
- switch (month) {
- case 12 :
- sum += 30;
- case 11 :
- sum += 31;
- case 10 :
- sum += 30;
- case 9 :
- sum += 31;
- case 8 :
- sum += 31;
- case 7 :
- sum += 30;
- case 6 :
- sum += 31;
- case 5 :
- sum += 30;
- case 4 :
- sum += 31;
- case 3 :
- if (((year % 4 == 0) && (year % 100 != 100)) || (year % 400 == 0)) {
- sum += 29;
- }else {
- sum += 28;
- }
- case 2 :
- sum += 31;
- case 1 :
- sum += day;
- break;
- default :
- System.out.println("输入日期非法");
- break;
- }
- System.out.println("今天是" + year + "年的第" + sum + "天");
- }
- }
- class SwitchTest41 {
- public static void main(String[] args) {
- int year = Integer.parseInt(args[0]);
- int month = Integer.parseInt(args[1]);
- int day = Integer.parseInt(args[2]);
- int sum = day;
- switch (month -1) {
- case 11 :
- sum += 30;
- case 10 :
- sum += 31;
- case 9 :
- sum += 30;
- case 8 :
- sum += 31;
- case 7 :
- sum += 31;
- case 6 :
- sum += 30;
- case 5 :
- sum += 31;
- case 4 :
- sum += 30;
- case 3 :
- sum += 31;
- case 2 :
- sum += 28;
- if (((year % 4 == 0) && (year % 100 != 100)) || (year % 400 == 0)) {
- sum += 1;
- }
- case 1 :
- sum += 31;
- break;
- default :
- System.out.println("输入日期非法");
- break;
- }
- System.out.println("今天是" + year + "年的第" + sum + "天");
- }
- }