//需求:一年有12个月,分属于春夏秋冬四个季节,键盘录入一个月份,用程序判断该月份属于哪个季节,并输出
//分析:
1.使用键盘录入的方式来获取值,导包,创建对象,接收数据
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();
2.多情况判断,采用switch语句实现
switch (month) {
case 3:
System.out.println("春季");
break;
case 6:
System.out.println("夏季");
break;
case 9:
System.out.println("秋季");
break;
case 12
System.out.println("冬季");
break;
default:
}
完整代码:
import java.util.Scanner;
public class switchDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个月份");
int month = sc.nextInt();
switch(month) {
case 1:
System.out.println("冬季");
break;
case 2:
System.out.println("冬季");
break;
case 3:
System.out.println("春季");
break;
case 4:
System.out.println("春季");
break;
case 5:
System.out.println("春季");
break;
case 6:
System.out.println("夏季");
break;
case 7:
System.out.println("夏季");
break;
case 8:
System.out.println("夏季");
break;
case 9:
System.out.println("秋季");
break;
case 10:
System.out.println("秋季");
break;
case 11:
System.out.println("秋季");
break;
case 12:
System.out.println("冬季");
break;
default:
System.out.println("你输入的月份有误");
break; //可以省略掉
}
}