关于循环结构 判断月份的两种方法

方法一:

import java.util.*;
public class Demon02 {
//输入数字代表月份 判断季节
public static void main(String[] args){
Scanner input=new Scanner(System.in);//创建扫描仪对象
System.out.println("请输入数字");
int month=input.nextInt();
String season="";//保障输入的是1-12月
if(month>12||month<1){
System.out.println("输入月份不存在");
}else {// 12 1 2月为冬天 , 3 4 5为春天, 6 7 8为夏天 ,9 10 11为秋天
if(month>=1&&month==12){

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("秋天");
}
}
}
}

方法二:

import java.util.*;
public class Demon2 {
public static void main(String[] args){
System.out.println("请输入数字:");
Scanner input=new Scanner(System.in);
int month=input.nextInt();
String season="";

switch(month){
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.print("秋季");
break;
default:
System.out.println("月份不存在");
}
}

}

 

posted on 2018-07-18 16:34  Sjm19961026  阅读(354)  评论(0编辑  收藏  举报

导航