java学习之路之基本语法-程序流程控制-switch语句练习题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qintian888/article/details/53968310
switch(变量){
case 常量1:
语句1;
break;
case 常量2:
语句2;
break;
… …
case 常量N:
语句N;
break;
default:
语句;
break;
 }



[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class SwitchTest {  
  2.       
  3.     public static void main(String[] args) {  
  4.         /* 
  5.         switch (变量) { 变量的数据类型必须是byte,char,short,int, String, 枚举对象 
  6.             case 常量1 : // if (变量 == 常量1) 
  7.                 语句1; 
  8.                 break; // 如果没有break语句, switch不是严格的分支 
  9.             case 常量2 : // else if (变量 == 常量2) 
  10.                 语句2; 
  11.                 break; 
  12.         } 
  13.         */  
  14.         int a = 20;  
  15.         switch (a) {  
  16.             case 10 : // if (a == 10)  
  17.                 System.out.println("a==10");  
  18.                 break// 中断, 打断, 破坏  
  19.             case 20 :  
  20.                 System.out.println("a==20");  
  21.                 break// 中断, 打断, 破坏,   
  22.                 //如果没有break, 一旦进入case,导致后面的所有case都能进入, 直到遇到break或switch结束  
  23.             case 30 :  
  24.                 System.out.println("a==30");  
  25.                 break// 中断, 打断, 破坏  
  26.             default : // 缺省 相当于 else   
  27.                 System.out.println("default");  
  28.                 break// 中断, 打断, 破坏  
  29.         }  
  30.         System.out.println("after switch");   
  31.           
  32.   
  33.     }  
  34. }  
  35.   
  36. /* 
  37. 使用switch语句改写下列if语句: 
  38.  int a = 3; 
  39.  int x = 100; 
  40.  if(a==1) 
  41.     x+=5; 
  42.  else if(a==2) 
  43.     x+=10; 
  44.  else if(a==3) 
  45.     x+=16; 
  46.  else        
  47.     x+=34; 
  48. System.out.println(x); 
  49. */  
  50. class Exer4 {  
  51.       
  52.     public static void main(String[] args) {  
  53.         int a = 3;  
  54.         int x = 100;  
  55.         switch (a) {  
  56.             case 1 :  
  57.                 x += 5;  
  58.                 break;  
  59.             case 2 :  
  60.                 x += 10;  
  61.                 break;  
  62.             case 3 :  
  63.                 x += 16;  
  64.                 break;  
  65.             default :  
  66.                 x += 34;  
  67.                 break;  
  68.         }  
  69.         System.out.println(x);  
  70.     }  
  71. }  
  72.   
  73. class SwitchTest2 {  
  74.     //根据从命令行参数获取的月份,打印该月份所属的季节。  
  75.     //3,4,5 春季 6,7,8 夏季  9,10,11 秋季 12, 1, 2 冬季  
  76.     public static void main(String[] args) {  
  77.         int month = Integer.parseInt(args[0]);  
  78.         switch (month) {  
  79.             case 1 :  
  80.             case 2 :  
  81.             case 12 :  
  82.                 System.out.println("冬季");  
  83.                 break;  
  84.             case 3 :  
  85.             case 4 :  
  86.             case 5 :  
  87.                 System.out.println("春季");  
  88.                 break;  
  89.             case 6 :  
  90.             case 7 :  
  91.             case 8 :  
  92.                 System.out.println("夏季");  
  93.                 break;  
  94.             case 9 :  
  95.             case 10 :  
  96.             case 11 :  
  97.                 System.out.println("秋季");  
  98.                 break;  
  99.             default :  
  100.                 System.out.println("月份错误");  
  101.                 break;  
  102.         }  
  103.   
  104.     }  
  105. }  
  106.   
  107.   
  108. /* 
  109. 使用 switch 把小写类型的 char型转为大写。只转换 a, b, c, d, e. 其它的输出 “other”。 
  110. 提示:接收命令行参数的字符方法 
  111. char ch = args[0].charAt(0); 
  112. */  
  113. public class SwitchTest1 {  
  114.       
  115.     public static void main(String[] args) {  
  116.         char ch = args[0].charAt(0);  
  117.           
  118.         switch (ch) {  
  119.             case 'a' :  
  120.                 System.out.println("A");  
  121.                 break;  
  122.             case 'b' :  
  123.                 System.out.println("B");  
  124.                 break;  
  125.             case 'c' :  
  126.                 System.out.println("C");  
  127.                 break;  
  128.             case 'd' :  
  129.                 System.out.println("D");  
  130.                 break;  
  131.             case 'e' :  
  132.                 System.out.println("E");  
  133.                 break;  
  134.             default :  
  135.                 System.out.println("other");  
  136.                 break;  
  137.         }  
  138.     }  
  139. }  
  140.   
  141. class SwitchTest11 {  
  142.       
  143.     public static void main(String[] args) {  
  144.         char ch = args[0].charAt(0);  
  145.         String str = "Other"// 先假设一种输出内容  
  146.         switch (ch) {  
  147.             case 'a' :  
  148.             case 'b' :  
  149.             case 'c' :  
  150.             case 'd' :  
  151.             case 'e' :  
  152.                 str = "" + (char)(ch - 32); // 如果假设不对, 则对之进行修改  
  153.         }  
  154.         System.out.println(str); // 统一输出即可  
  155.     }  
  156. }  
  157.   
  158.   
  159. //对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。  
  160. public class SwitchTest2 {  
  161.       
  162.     public static void main(String[] args) {  
  163.         int score = Integer.parseInt(args[0]);  
  164.           
  165.         switch (score / 10) {  
  166.             case 0 :  
  167.             case 1 :  
  168.             case 2 :  
  169.             case 3 :  
  170.             case 4 :  
  171.             case 5 :  
  172.                 System.out.println("不及格");  
  173.                 break;  
  174.             case 6 :  
  175.             case 7 :  
  176.             case 8 :  
  177.             case 9 :  
  178.                 System.out.println("及格");  
  179.                 break;  
  180.             default :  
  181.                 if (score == 100) {  
  182.                     System.out.println("及格");  
  183.                     break;  
  184.                 }  
  185.                 System.out.println("输入成绩非法");  
  186.         }  
  187.     }  
  188. }  
  189.   
  190. /* 
  191. 编写程序:从命令行参数中获取一个学生成绩,存放在变量score中,根据score的值输出 
  192. 其对应的成绩等级: 
  193. score>=90        等级:A 
  194. 70=<score<90     等级: B 
  195. 60=<score<70     等级: C 
  196. score<60         等级:D 
  197. int score = Integer.parseInt(args[0]); 
  198. */  
  199. public class SwitchTest3 {  
  200.       
  201.     public static void main(String[] args) {  
  202.         int score = Integer.parseInt(args[0]);  
  203.           
  204.         switch (score / 10) {  
  205.             case 9 :  
  206.                 System.out.println("等级:A");  
  207.                 break;  
  208.             case 8 :  
  209.             case 7 :  
  210.                 System.out.println("等级:B");  
  211.                 break;  
  212.             case 6 :  
  213.                 System.out.println("等级:C");  
  214.                 break;  
  215.             case 5 :  
  216.             case 4 :  
  217.             case 3 :  
  218.             case 2 :  
  219.             case 1 :  
  220.             case 0 :  
  221.                 System.out.println("等级:D");  
  222.                 break;  
  223.             default :  
  224.                 if (score == 100){  
  225.                     System.out.println("等级:A");  
  226.                     break;  
  227.                 }  
  228.                 System.out.println("输入成绩非法");  
  229.                 break;  
  230.         }  
  231.     }  
  232. }  
  233.   
  234. /* 
  235. 接收命令行参数年、月、日,判断这一天是当年的第几天 
  236.    注:判断一年是否是闰年的标准: 
  237.        1)可以被4整除,但不可被100整除 
  238.        2)可以被400整除     
  239. */  
  240. public class SwitchTest4 {  
  241.       
  242.     public static void main(String[] args) {  
  243.         int year = Integer.parseInt(args[0]);  
  244.         int month = Integer.parseInt(args[1]);  
  245.         int day = Integer.parseInt(args[2]);  
  246.         int sum = 0;  
  247.           
  248.         switch (month) {  
  249.             case 12 :  
  250.                 sum += 30;  
  251.             case 11 :  
  252.                 sum += 31;  
  253.             case 10 :  
  254.                 sum += 30;  
  255.             case 9 :  
  256.                 sum += 31;  
  257.             case 8 :  
  258.                 sum += 31;  
  259.             case 7 :  
  260.                 sum += 30;  
  261.             case 6 :  
  262.                 sum += 31;  
  263.             case 5 :  
  264.                 sum += 30;  
  265.             case 4 :  
  266.                 sum += 31;  
  267.             case 3 :  
  268.                 if (((year % 4 == 0) && (year % 100 != 100)) || (year % 400 == 0)) {  
  269.                     sum += 29;  
  270.                 }else {  
  271.                     sum += 28;  
  272.                 }  
  273.             case 2 :  
  274.                 sum += 31;  
  275.             case 1 :  
  276.                 sum += day;  
  277.                 break;  
  278.             default :  
  279.                 System.out.println("输入日期非法");  
  280.                 break;  
  281.         }  
  282.         System.out.println("今天是" + year + "年的第" + sum + "天");  
  283.     }   
  284. }  
  285.   
  286. class SwitchTest41 {  
  287.       
  288.     public static void main(String[] args) {  
  289.         int year = Integer.parseInt(args[0]);  
  290.         int month = Integer.parseInt(args[1]);  
  291.         int day = Integer.parseInt(args[2]);  
  292.         int sum = day;  
  293.           
  294.         switch (month -1) {  
  295.             case 11 :  
  296.                 sum += 30;  
  297.             case 10 :  
  298.                 sum += 31;  
  299.             case 9 :  
  300.                 sum += 30;  
  301.             case 8 :  
  302.                 sum += 31;  
  303.             case 7 :  
  304.                 sum += 31;  
  305.             case 6 :  
  306.                 sum += 30;  
  307.             case 5 :  
  308.                 sum += 31;  
  309.             case 4 :  
  310.                 sum += 30;  
  311.             case 3 :  
  312.                 sum += 31;  
  313.             case 2 :  
  314.                 sum += 28;  
  315.                 if (((year % 4 == 0) && (year % 100 != 100)) || (year % 400 == 0)) {  
  316.                     sum += 1;  
  317.                 }  
  318.             case 1 :  
  319.                 sum += 31;  
  320.                 break;  
  321.             default :  
  322.                 System.out.println("输入日期非法");  
  323.                 break;  
  324.         }  
  325.         System.out.println("今天是" + year + "年的第" + sum + "天");  
  326.     }   
  327. }  

猜你喜欢

转载自blog.csdn.net/qintian888/article/details/53968310