Java中的Switch用法

来源:互联网 发布:影楼相册制作软件 编辑:程序博客网 时间:2024/06/01 10:29

1,在java中switch后的表达式的类型只能为以下几种:byte、short、char、int(在Java1.6中是这样),   在java1.7后支持了对string的判断 

public class TestSwitch{    public static void main(String args[]){        char c = 'a';        //char类型字符        switch(c){            default:                System.out.println("打印默认值");                break;            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;                    }    }}

2,在java中如果switch的case语句中少写了break;这个关键字,在编译的时候并没有报错,但是在执行的时候会一直执行所有case条件下的语句并不是去判断,所以会一直执行直到遇到break关键字跳出

[java] view plain copy
  1. package sampleTest;  
  2.   
  3. public class TestSwitch {  
  4.     public static void main(String[] args) {  
  5.         int count = 0;  
  6.         switch (count) {  
  7.         case 0:  
  8.             System.out.println("case0");  
  9.         case 1:  
  10.             System.out.println("case1");  
  11.         case 2:  
  12.             System.out.println("case2");  
  13.             break;  
  14.         case 3:  
  15.             System.out.println("case3");  
  16.         default:  
  17.             System.out.println("default!");  
  18.         }  
  19.           
  20.         System.out.println("-------------------");  
  21.           
  22.         String msg = "dragon";  
  23.         switch (msg) {  
  24.         case "rabbit":  
  25.             System.out.println("rabbit ");  
  26.         case "dragon":  
  27.             System.out.println("happy new year");  
  28.         default:  
  29.             System.out.println("what ?");  
  30.         case "monkey":  
  31.             System.out.println("monkey");  
  32.             break;  
  33.         case "tiger":  
  34.             System.out.println("tiger!!");  
  35.         }  
  36.     }  
  37. }  

输出如下:
case0
case1
case2
-------------------
happy new year
what ?
monkey

上面例子说明了两个问题,第一个是不加break的后果,第二个是default的位置对执行的影响

3,多个case输出相同,可以只写一个case,如下面这个输出月份的天数的经典问题

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace A_Month_Has_Days_Modified  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Console.WriteLine("请输入你要查询的年份");  
  13.             int year = Convert.ToInt32(Console.ReadLine());  
  14.             Console.WriteLine("请输入你要查询的月份");  
  15.             int month = Convert.ToInt32(Console.ReadLine());  
  16.             int bigDay = 31, smallDay = 30, leapDay = 29, nonleapDay = 28;  
  17.   
  18.             bool isLeapYear;  
  19.             if ((year % 400 == 0) || ((year % 4 != 0) && (year % 100 == 0)))  
  20.             {  
  21.                 isLeapYear = true;  
  22.             }  
  23.             else  
  24.             {  
  25.                 isLeapYear = false;  
  26.             }  
  27.             switch (month)  
  28.             {  
  29.                 case 1:  
  30.                 case 3:  
  31.                 case 5:  
  32.                 case 7:  
  33.                 case 8:  
  34.                 case 10:  
  35.                 case 12:  
  36.                     Console.WriteLine("{0}年{1}月共有{2}天", year, month, bigDay);  
  37.                     break;  
  38.                 case 4:  
  39.                 case 6:  
  40.                 case 9:  
  41.                 case 11:  
  42.                     Console.WriteLine("{0}年{1}月共有{2}天", year, month, smallDay);  
  43.                     break;  
  44.                 case 2:  
  45.                     if (isLeapYear == true)  
  46.                     {  
  47.                         Console.WriteLine("{0}年{1}月共有{2}天", year, month, leapDay);  
  48.                     }  
  49.                     else  
  50.                     {  
  51.                         Console.WriteLine("{0}年{1}月共有{2}天", year, month, nonleapDay);  
  52.                     }  
  53.                     break;                 
  54.                 default:  
  55.                     Console.WriteLine("您输入的年份或月份格式不正确,年份为四位数字,月份为1至12");  
  56.                     break;  
  57.             }  
  58.             Console.ReadKey();  
  59.         }  
  60.     }  
  61. }