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] viewplain 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] viewplain 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();
}
}
}
相关文章推荐
- Java switch的用法与case的穿透现象举例详解
- Java switch-case 的特殊用法
- java中使用switch-case的用法及注意事项超全总结
- Java中switch用法
- Java中的Switch用法
- Java Switch的用法
- Java中使用switch-case的用法及注意事项
- Java中使用switch-case的用法及注意事项
- Java中Switch用法代码示例
- Java的switch用法
- java 中switch基本用法
- switch…case语句在C#和Java中用法的区别?
- Java之戳中痛点 - (3)三目运算符的两个操作数类型尽量一致 Java之戳中痛点 - (4)i++ 和 ++i 探究原理 Java之戳中痛点 - (1)易变业务使用脚本语言编写 Java之戳中痛点 - (2)取余用偶判断,不要用奇判断 (5)switch语句break不能忘以及default不同位置的用法 Java之戳中痛点 - (7)善用Java整型缓存池
- Java中的Switch用法
- Java中使用switch-case的用法及注意事项
- java_switch在没有break时的用法
- java中switch和break用法简介
- Java中使用switch-case的用法及注意事项
- 【JAVA基础】switch case语句用法
- Java中Switch Case语句的用法