Java实现输入日期计算该日是所输入年份的第多少天

Java实现输入日期计算该日是所输入年份的第多少天

思路:将该月前所有月份的天数加起来再加上输入的号数即为所求
Ps:闰年能被4和400整除,不能被100整除
代码如下

/*
 从键盘上输入"year","month"和"day",要求通过程序输出输入的日期为XXXX年的第几天
 */
import java.util.Scanner;   //导入Scanner包
public class SwitchCaseExer {
    public static void main(String[]args){
        Scanner scan = new Scanner(System.in);      //Scanner方法调用

        System.out.println("请输入年份:");
        int year = scan.nextInt();                  //变量year的创建及输入

        System.out.println("请输入月份:");
        int month = scan.nextInt();                 //变量month的创建及输入
        int newMonth = month;                       //month经过for遍历后值会发生改变,所以创建个新变量用来接收month的原始值

        System.out.println("请输入号数:");
        int day = scan.nextInt();                   //变量day的创建及输入

        int monthDay = 0;
        int days = 0;                                  //初始化变量monthDay(每个月的天数)以及总天数days
        for (--month; month > 0; month--) {             //通过遍历当前月份之前的所有月份来获取之前的天数
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    monthDay = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    monthDay = 30;
                    break;
                case 2:
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                        //闰年的判断
                        monthDay = 29;
                    else
                        monthDay = 28;
                    break;
                default:
                    System.out.println("日期输入错误!");
                }
            days += monthDay;           //当前月之前的天数和
            }
            days = days + day;          //总天数
            System.out.print(year + "年" + newMonth + "月" + day + "日是");
            System.out.println(year + "年" + "的第" + days + "天");

        scan.close();
    }
}

以上代码仅供参考


--------------------------------------------------------------------------------------------

上述代码仍有问题,输入超出正常范围的值不能识别,且不能重复输入判定,以下为改良后的代码:

import java.util.Scanner;

public class CountDaysOfYear {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean flag = true;
        while(flag) {
            System.out.print("请输入年份(输入0退出):");
            int year = input.nextInt();
            if (year < 0){
                System.out.println("年份输入错误,请重新输入!");
                continue;
            } else if (year == 0) {
                flag = false;
                break;
            }

            System.out.print("请输入月份(输入0退出):");
            int month = input.nextInt();
            if (month > 12) {
                System.out.println("月份输入错误,请重新输入!");
                continue;
            } else if (month == 0){
                flag = false;
                break;
            }else if (month < 0) {
                System.out.println("月份输入错误,请重新输入!");
                continue;
            }

            System.out.print("请输入号数(输入0退出):");
            int day = input.nextInt();
            if (day == 0) {
                flag = false;
                break;
            } else if (day < 0) {
                System.out.println("号数输入错误,请重新输入!");
                continue;
            }

            int days = 0;
            int MonthDays = 0;
            int NewMonth = month;

            for (--NewMonth; NewMonth >= 1; NewMonth--) {	//累加当前月份前所有天数的总和
                switch (NewMonth) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        MonthDays = 31;
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        MonthDays = 30;
                        break;
                    case 2:
                        if (year % 100 != 0 && year % 4 == 0 || year % 400 == 0){
                            MonthDays = 29;
                            break;
                        }
                        MonthDays = 28;
                        break;
                }
                days += MonthDays;
            }

            switch (month) {	//判断输入数值是否正常
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    if (day > 31) {
                        System.out.println("号数输入错误,请重新输入!");
                        continue;
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    if (day > 30) {
                        System.out.println("号数输入错误,请重新输入!");
                        continue;
                    }
                    break;
                case 2:
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                        if (day > 29) {
                            System.out.println("号数输入错误,请重新输入!");
                            continue;
                        }
                        break;
                    } else {
                        if (day > 28) {
                            System.out.println("号数输入错误,请重新输入!");
                            continue;
                        }
                        break;
                    }
            }
            days += day;	//当前月之前总天数加上该月天数即为结果
            System.out.println(year + "年" + month + "月" + day + "日是" +
                    year + "年第" + days + "天");
        }
        input.close();
    }
}

Java实现输入日期计算该日是所输入年份的第多少天 Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天 Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天Java实现输入日期计算该日是所输入年份的第多少天
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

离歌慢饮

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

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

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

打赏作者

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

抵扣说明:

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

余额充值