import java.util.Scanner;
/**
* (5)根据指定月份,打印该月份所属的季节。
* 3,4,5 春季
* 6,7,8 夏季
* 9,10,11 秋季
* 12, 1, 2 冬季
* 要求if和switch各写一版
*/
public class Season3 {
public static void main(String[] args) {
int month = scanner();
session(month);
}
private static int scanner(){
int month=0;
while (true) {
try {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个月份值");
month = scanner.nextInt();
break;
} catch (Exception e) {
System.out.println("请不要输入和月份无关的字");
}
}
return month;
}
private static void session(int month){
while (true) {
try {
if(month==3||month==4||month==5){
System.out.println(month+"月份是春季");
break;
}
if(month==6||month==7||month==8){
System.out.println(month+"月份是夏季");
break;
}
if(month==9||month==10||month==11){
System.out.println(month+"月份是秋季");
break;
}
if(month==12||month==1||month==2){
System.out.println(month+"月份是冬季");
break;
}//if if没有短路的意思,就算是匹配到了,也都还会继续判断,而if()else if(){}则只要一个匹配到了,就不会再执行之后的判断了
//所以,依据自己的需要,我们需要合理的使用if或if else 一般使用if else的概率会多些
else{
System.out.println(month+"月份,怕你是来自外星球的吧");
month = scanner();
}
} catch (Exception e) {
month = scanner();
}
}
}
}
日常Java代码练习(根据指定月份,打印该月份所属的季节。)
于 2021-06-21 18:41:13 首次发布