Java实现-循环输入月份,并判断输入的月份属于春夏秋冬哪个季节范围

代码实现

        思想:利用if-else语句分层次判断即可。

import java.util.Scanner;
public class homework_2_monthji { //homework_2_monthji为类名,可修改为对应创建的类名
    public static void main(String[] args) {
        /*
        程序功能:循环输入月份,并判断输入的月份属于春夏秋冬哪个季节范围
        气象划分的季节如下:
        春:3、4、5月
        夏:6、7、8月
        秋:9、10、11月
        冬:12、1、2月
         */
        int month;
        System.out.println("请在1-12月中输入您想判断季节的月份,共有12次机会:");
        Scanner scanner=new Scanner(System.in);
        int i;
        for(i=0;i<12;i++){
            month=scanner.nextInt();
            //实现月份、季节判断的方法还可以用switch语句或改进其他
            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("当前输入的月份属于秋季");
            }
            else if(month==1 || month==2 || month==12){
                System.out.println("当前输入的月份属于冬季");
            }
            else{
                System.out.println("您输入的月份错误,无法判断所属季节,请重新输入");
            }
        }
    }
}

运行结果(部分)

猜你喜欢

转载自blog.csdn.net/m0_54158068/article/details/124393734