2024年12月12日 建站教程
switch语句是一个多路分支语句。它提供了一种简单的方法,可以根据表达式的值将执行分派到代码的不同部分。基本上,表达式可以是byte、short、char和int原始数据类型。从JDK7开始,它还适用于枚举类型(Java中的Enums)、String类和Wrapper类。
java语法中怎么使用switch语句,下面web建站小编给大家简单介绍一下!
具体示列如下:
public class Test { public static void main(String[] args) { int day = 2; String dayType; String dayString; switch (day) { case 1: dayString = "Monday"; break; case 2: dayString = "Tuesday"; break; case 3: dayString = "Wednesday"; break; case 4: dayString = "Thursday"; break; case 5: dayString = "Friday"; break; case 6: dayString = "Saturday"; break; case 7: dayString = "Sunday"; break; default: dayString = "Invalid day"; } switch (day) { // multiple cases without break statements case 1: case 2: case 3: case 4: case 5: dayType = "Weekday"; break; case 6: case 7: dayType = "Weekend"; break; default: dayType = "Invalid daytype"; } System.out.println(dayString + " is a " + dayType); } }
本文链接:http://so.lmcjl.com/news/19415/