输入某年某月某日,用Java语句来判断这天是这一年的第几天

 

package test;

import java.util.Scanner;

public class zuoye {
    public static void main(String[] args) {
        Scanner n = new Scanner(System.in);
        int year, month, day = 0;
        int days;
        int e = 0;// 用来进行判断的
        int d = 0;// 累计天数
        do {
            System.out.println("输入年月日:");
            year = n.nextInt();
            month = n.nextInt();
            days = n.nextInt();
            if (year < 0 || month < 0 || month > 12 || days < 0 || days > 31) {
                System.out.println("输入错误");
                e = 1;// 等于1时,说明输入有误6
            }
            // 年月日大于0
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                if (days <= 31) {
                    break;
                } else {
                    System.out.println("本月不超过31天");
                    e = 1;
                }
            }
            // 1,3,5,7,8,10,12月份不超过31天
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (days <= 30) {
                    break;
                } else {
                    System.out.println("本月不超过30天");
                    e = 1;
                }
            }
            // 4,6,9,11月份不超过30天
            if (month == 2) {
                if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                    if (days > 29) {
                        System.out.println("闰年2月不超过29天");
                        e = 1;
                    } else {
                        e = 0;
                    }
                }
                // 闰年2月不超过29天
                if (year % 4 != 0 || (year % 4 == 0 && year % 100 == 0)) {
                    if (days > 28) {
                        System.out.println("非闰年2月不超过28天");
                        e = 1;
                    } else {
                        e = 0;
                    }
                }
                // 非闰年2月不超过28天
            }
        } while (e == 1);// 判断等于1时,就继续输入
        for (int i = 1; i < month; i++) {
            switch (i) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                    day = 29;// 闰年2月有29天
                } else {
                    day = 28;// 非闰年2月有28天
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                day = 30;
                break;
            default:
                System.out.println("输入的月份有误");
            }
            d += day;
        }
        System.out.println("这是" + year + "年的第" + (d + days) + "天");

}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值