You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
710 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

public class Jb09_SwitchDemo
{
public static void main(String[] args)
{
// 选择结构Switch
/*
switch (表达式)
{
case 取值1:
执行语句;
break;
case 取值2:
执行语句2;
break;
......
default:
执行语句;
break;
}
*/
//根据输入数字,输出对应季节
int month = 3;
switch (month)
{
case 3:
case 4:
case 5:
System.out.println("是春季");
break;
default:
System.out.println("没有对应的季节");
break; //此处break可省略
}
/*
if与switch的应用场景
if:
1、对具体的值进行判断
2、对区间判断
3、对运算结果是boolean类型的表达式进行判断
switch
1、对具体的值进行判断
2、值的个数是固定的对于只有几个固定的数值进行判断建议用switch效率更高。
*/
}
}