Java学习记录 求输入月份,判断该月所处的季节并输出季节

2021-04-03 选择结构训练任务 3

要求输入月份,判断该月所处的季节并输出季节(假设:12、1、2 月为冬季,依次类推)

 

使用switch语句case的穿透效果,当月份为12,1,2时,打印输出为冬季。当月份为3,4,5时,打印输出为春季。当月份为6,7,8时,打印输出为夏季。当月份为9,10,11时,打印输出为秋季。

代码如下:

import java.util.Scanner;

public class Class4_3 {

	public static void main(String[] args) {
		
		int inputMonth ;
		
		Scanner input = new Scanner(System.in);//获取输入
		System.out.println("请输入月份(1~12):");
		//判断输入数据
		if(input.hasNextInt()) {
			inputMonth = input.nextInt();//赋值
			//判断月份范围
			if(inputMonth>0&&inputMonth<13) {
				switch(inputMonth) {
				case 12:case 1:case 2:
					System.out.println(inputMonth+"月是冬季");break;
				case 3:case 4:case 5:
					System.out.println(inputMonth+"月是春季");break;
				case 6:case 7:case 8:
					System.out.println(inputMonth+"月是夏季");break;
				case 9:case 10:case 11:
					System.out.println(inputMonth+"月是秋季");break;
				}
			}else {
				System.out.println("你输入的月份超出范围 "+inputMonth);
			}
			
		}else {
			System.out.println("你输入的数据有误");
		}

	}

}

运行结果

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值