java语句(Switch练习)-菜鸟学习java第十九天笔记
语句(Switch练习)
//需求2:打印月份所属季节(3-4-5春季,6-7-8夏季,9-10-11秋季,12-1-2冬季)
class Test
{
public static void main(String[] args)
{
int x = 4;
switch(x)
{
case 3:
case 4:
case 5:
System.out.println(x+"月是春季");
break;
case 6:
case 7:
case 8:
System.out.println(x+"月是夏季");
break;
case 9:
case 10:
case 11:
System.out.println(x+"月是秋季");
break;
case 12:
case 1:
case 2:
System.out.println(x+"月是冬季");
break;
default:
System.out.println("错误的月份");
}
}
}
问1:什么时候用switch语句?
答1:当判断的值不多,且数据类型是byte、short、int或char
-----------------------------------------------------------------------------------------------------------------------------------------------
03天-01-语句(while)
while(条件表达式)
{
执行语句;
}
//打印1,3,5,7,9
class Test
{
public static void main(String[] args)
{
int x=1;
while(x<=10)
{
System.out.println("x="+x);
x+=2; //修改循环变量。Ctrl+C 强制结束循环
}
}
}
本文来源 我爱IT技术网 http://www.52ij.com/jishu/5549.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
