判断月份是否为春季,夏季,秋季,冬季
12月-2月为冬季;
3月-5为春季;
6月-8为夏季;
9月-11月为秋季;
第一种写法—简单易繁琐
import java.util.Scanner;
class a{
public static void main(String[] args){
Scanner xs=new Scanner(System.in);
System.out.println("请输入月份");
int month=xs.nextInt();
if(month<=1 && month>=12){
System.out.println("你输入的月份有误");
}else if(month==1){
System.out.println("冬季");
}else if(month==2){
System.out.println("冬季");
}else if(month==3){
System.out.println("春季");
}else if(month==4){
System.out.println("春季");
}else if(month==5){
System.out.println("春季");
}else if(month==6){
System.out.println("夏季");
}else if(month==7){
System.out.println("夏季");
}else if(month==8){
System.out.println("夏季");
}else if(month==9){
System.out.println("秋季");
}else if(month==10){
System.out.println("秋季");
}else if (month==11){
System.out.println("秋季");
}else if(month==12){
System.out.println("冬季");
}
}
}
第二种写法---比较繁琐
if(month<=1 && month>=12){
System.out.println("你输入的月份有误");
}
else if(month==12 || month==1 || month==2){
System.out.println("冬季");
}
else if(month==3 || month==4 || month==5){
System.out.println("春季");
}
else if(month==6 || month==7 || month==8){
System.out.println("夏季");
}
else if(month==9 || month==10 || month==11){
System.out.println("秋季");
}
第三种写法–简单–采用逻辑结构写法----不繁琐—采用第三种写法
if(month<=1 && month>=12){
System.out.println("你输入的月份有误");
}else if(month>=12 && month<=2){
System.out.println("冬季");
}
else if(month>=3 && month<=5){
System.out.println("夏季");
}
else if(month>=6 && month<=8){
System.out.println("夏季");
}
else if(month>=9 && month<=11){
System.out.println("秋季");
}