您的位置:首页 > 编程语言 > Java开发

Java中的Switch用法

2017-08-15 10:55 274 查看

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

package sampleTest;  

  

public class TestSwitch {  

    public static void main(String[] args) {  

        int count = 0;  

        switch (count) {  

        case 0:  

            System.out.println("case0");  

        case 1:  

            System.out.println("case1");  

        case 2:  

            System.out.println("case2");  

            break;  

        case 3:  

            System.out.println("case3");  

        default:  

            System.out.println("default!");  

        }  

          

        System.out.println("-------------------");  

          

        String msg = "dragon";  

        switch (msg) {  

        case "rabbit":  

            System.out.println("rabbit ");  

        case "dragon":  

            System.out.println("happy new year");  

        default:  

            System.out.println("what ?");  

        case "monkey":  

            System.out.println("monkey");  

            break;  

        case "tiger":  

            System.out.println("tiger!!");  

        }  

    }  

}  

输出如下:

case0

case1

case2

-------------------

happy new year

what ?

monkey

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

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

[csharp] view
plain copy

using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

  

namespace A_Month_Has_Days_Modified  

{  

    class Program  

    {  

        static void Main(string[] args)  

        {  

            Console.WriteLine("请输入你要查询的年份");  

            int year = Convert.ToInt32(Console.ReadLine());  

            Console.WriteLine("请输入你要查询的月份");  

            int month = Convert.ToInt32(Console.ReadLine());  

            int bigDay = 31, smallDay = 30, leapDay = 29, nonleapDay = 28;  

  

            bool isLeapYear;  

            if ((year % 400 == 0) || ((year % 4 != 0) && (year % 100 == 0)))  

            {  

                isLeapYear = true;  

            }  

            else  

            {  

                isLeapYear = false;  

            }  

            switch (month)  

            {  

                case 1:  

                case 3:  

                case 5:  

                case 7:  

                case 8:  

                case 10:  

                case 12:  

                    Console.WriteLine("{0}年{1}月共有{2}天", year, month, bigDay);  

                    break;  

                case 4:  

                case 6:  

                case 9:  

                case 11:  

                    Console.WriteLine("{0}年{1}月共有{2}天", year, month, smallDay);  

                    break;  

                case 2:  

                    if (isLeapYear == true)  

                    {  

                        Console.WriteLine("{0}年{1}月共有{2}天", year, month, leapDay);  

                    }  

                    else  

                    {  

                        Console.WriteLine("{0}年{1}月共有{2}天", year, month, nonleapDay);  

                    }  

                    break;                 

                default:  

                    Console.WriteLine("您输入的年份或月份格式不正确,年份为四位数字,月份为1至12");  

                    break;  

            }  

            Console.ReadKey();  

        }  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: