Java基础题(4)

题目:
输入某年某月某日,判断这一天是这一年的第几天?
分析:、
闰年二月29,普通年份28
1月31;2月 ,3月31;4月30;5月31;6月30;7月31;
8月31;9月30;10月31;11月30;12月31
代码:

import java.util.Scanner;

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int year, month, day;
        do {
            System.out.println("输入年份:");
            year = s.nextInt();
            if (year < 1) {
                System.out.println("请输入正确年份");
                continue;
            } else
                break;
        } while (true);
        do {
            System.out.println("输入月份:");
            month = s.nextInt();
            if (month < 1 || month > 12) {
                System.out.println("请输入正确月份");
                continue;
            } else
                break;
        } while (true);
        do {
            System.out.println("输入日:");
            day = s.nextInt();
            if (isDays(year, month, day))
                break;
        } while (true);
        //已经输入正确是年月日,进行判断并累加天数
        int sum = day;//本月日期
        //从12月倒叙进行穿透累加
        switch (month - 1) {//-1为减除本月日期
            case 11:
                sum += 30;
            case 10:
                sum += 31;
            case 9:
                sum += 30;
            case 8:
                sum += 31;
            case 7:
                sum += 31;
            case 6:
                sum += 30;
            case 5:
                sum += 31;
            case 4:
                sum += 30;
            case 3:
                sum += 31;
            case 2:
                if (year % 4 == 0) {
                    sum += 29;
                } else {
                    sum += 28;
                }
            case 1:
                sum += 31;
        }
        System.out.println("这是今年的第" + sum + "天");
    }

    //判断日期是否正确
    public static boolean isDays(int year, int month, int day) {
        if (day < 1 || day > 31) {
            System.out.println("日期不正确,请从1~31中选取");
            return false;
        }
        int a = 2;//闰年(29)平年(28)不同
        int[] b = {1, 3, 5, 7, 8, 10, 12};//31天上限的月份
        int[] c = {4, 6, 9, 11};//30天上线的月份
        if(month==2){
            if (year%4==0){
                if(day>29){
                    System.out.println("闰年只有29天");
                    return false;
                }return true;
            }else {
                if(day>28){
                    System.out.println("平年只有28天");
                    return false;
                }return true;
            }
        }else if (findMonth(b,month)){
            if (day > 31) {
                System.out.println("这个月只有31天");
                return false;
            } else {
                return true;
            }
        }else  {
            if (day > 30) {
                System.out.println("这个月只有30天");
                return false;
            } else {
                return true;
            }
        }
    }
    //查找元素是否在数组中
    public static boolean findMonth(int[]b,int month){
        for (int i : b) {
            if(month==i)
                return true;
        }
        return false;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值