标题: switch语句的简单应用,想了半天终于做出来了 [打印本页] 作者: roybill 时间: 2015-5-12 22:13 标题: switch语句的简单应用,想了半天终于做出来了 /** 一年有12个月,每个月分别对应于不同的季节。
请根据给定的月份,输出对应的季节。
春:3,4,5
夏:6,7,8
秋:9,10,11
冬:1,2,12
*/
import java.util.Scanner;
class lingwuyier3
{
public static void main(String[] args)
{
/* int month=17;
switch(month)
{
case 3:
case 4:
case 5:
System.out.println(month+"月为春天");
break;
case 6:
case 7:
case 8:
System.out.println(month+"月为夏天");
break;
case 9:
case 10:
case 11:
System.out.println(month+"月为秋天");
break;
case 12:
case 1 :
case 2 :
System.out.println(month+"月为冬天");
break;
default :
System.out.println("非法季节");
break;
}
*/
Scanner in=new Scanner(System.in);
System.out.println("请输入月份(1-12):");
int month=in.nextInt();
switch(month)
{
case 3:
case 4:
case 5:
System.out.println(month+"月为春天");
break;
case 6:
case 7:
case 8:
System.out.println(month+"月为夏天");
break;
case 9:
case 10:
case 11:
System.out.println(month+"月为秋天");
break;
case 12:
case 1 :
case 2 :
System.out.println(month+"月为冬天");
break;
default :
System.out.println("非法季节");
break;
}
}
} 作者: Oh.Ba 时间: 2015-5-13 00:29
用for循环呀作者: 开到荼靡 时间: 2015-5-13 01:03