输入年,月,日输出这是该年第几天

代码如下:

import java.util.Scanner;

public class Day {
	
	public static void main(String[] args) {
		
		System.out.println("--------天数计算器--------");
		Scanner scanner = new Scanner(System.in);//新建键盘输入
//-------------------------------键盘输入限制-------------------------------------//		
		System.out.println("请输入年份:");
		int year = scanner.nextInt();//输入年
        while (year < 0){
        System.out.println("耶稣还没出生,请重新输入");
        	year = scanner.nextInt();
        }
//--------------------------------------------------------------------//
		System.out.println("请输入月份:");
		int month = scanner.nextInt();//输入月
		while(month > 12 || month <= 0){
			System.out.println("大哥请输入一个正确的月份");
			month = scanner.nextInt();
		}
//--------------------------------------------------------------------//
		System.out.println("请输入日期:");
		int day = scanner.nextInt();
		//判断输入的月份是否为大月
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
			//添加限制条件
			while(day <= 0 || day > 31){
				System.out.println("该月是大月,请重新输入一个1-31的日期");
				day = scanner.nextInt();
			}
		} else if (month == 4 || month == 6 || month == 9 || month == 11){//判断输入的月份是小月
			//添加限制条件
			while(day <= 0 || day > 30){
				System.out.println("该月是小月,请重新输入一个1-30的日期");
				day = scanner.nextInt();
			}
		} else {//为2月添加限制条件
			if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
				while (day <= 0 || day >=30){//闰年二月不超过29天
					System.out.println("这一年是闰年,请重新输入1-29之间的日期");
					day = scanner.nextInt();
				}
			} else {
				while (day <= 0 || day >= 29){//平年二月不超过28天
					System.out.println("这一年是平年,请重新输入1-28之间的日期");
					day = scanner.nextInt();
				}
			}
		}
//---------------------------计算代码-----------------------------------------//		
		//先if判断平年闰年 
		//平年:
		if (year % 4 != 0 && year % 100 == 0 && year % 400 != 0) {
			
		//switch选择月份
		switch (month) {
		//从12月往下穿透相加:
		case 12:case 10:case 7:case 5:
			day += 30;
		case 11:case 9:case 8:case 6:case 4:
			day += 31;
		
		case 3:
			day += 28;//平年2月有28天
        case 2:
            day += 31;
		case 1:
			System.out.println("这天是"+year+"年第" + day + "天");
			break;
		default:
			 System.out.println("错误的输入!!");//报错
			 break;}
		}

		else {
			//闰年
			switch (month) {
			//从12月往下穿透相加:
	        case 12:case 10:case 7:case 5:
			day += 30;
		    case 11:case 9:case 8:case 6:case 4:
			day += 31;
			
			case 3:
				day += 29;//闰年2月有29天
            case 2:
            day += 31;
			case 1:
				System.out.println("这天是"+year+"年第" + day + "天");
				break;

			default:
			 System.out.println("错误的输入!!");//报错
			 break;
			 }
	       }
	}

}

1、这里需要把输入内容作出限定包括年,月,日的限定,当需要限定输入内容时while循环可以实现判断错误并重新输入

2、switch中在case遇到break之前会发生穿透,运用倒序穿透相加可实现题目要求

效果如下

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玛卡巴卡yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值