Java实现从键盘分别输入年、月、日,判断这一天是当年的第几天

import java.util.Scanner;
class Exer1 {
	public static void main(String[] args) {
		Scanner scan = new Scanner (System.in);

		System.out.print("年:");
		int year = scan.nextInt();
		System.out.print("月:");
		int month = scan.nextInt();
		System.out.print("日:");
		int day = scan.nextInt();
		
		int days = day;//如果将days 初值设置为0,则可以加上case1: days += day;

		//加前面几个月的满月天数
		switch(month){
			case 12:
				//前面11个月的总天数
				//days += 第11月的天数;
				days += 30;
			case 11:
				//前面10个月的总天数
				//days += 第10月的天数;
				days += 31;
			case 10:
				days += 30;//九月
			case 9:
				days += 31;//八月
			case 8:
				days += 31;//七月
			case 7:
				days += 30;//六月
			case 6:
				days += 31;//五月
			case 5:
				days += 30;//四月
			case 4:
				days += 31;//三月
			case 3:
				days += 28;//二月
				/*if(闰年){
					days++;
				}
				*/
				if(year % 4 ==0 && year % 100 != 0 || year%400==0){
					days++;
				}
			case 2:
				days += 31;//一月
		}
		System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + days + "天");
	}	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值