使用java编程:输入一个年月日,求该年(月)的总天数。

/*

        1.输入年月日,求某年(月)的总天数。

*/

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.print("输入年月日,空格隔开:");
        ArrayList list = judge();
        int year = (int) list.get(0);
        int month = (int) list.get(1);
        int day = (int) list.get(2);
        System.out.print("请问想要求月还是年:");
        String choice = choose();
        boolean b =
                (year % 4 == 0 & year % 400 != 0) | (year % 400 == 0);
        if (b){
            System.out.println("该年为闰年");
        }else{
            System.out.println("该年为平年");
        }
        if (choice.equals("年")){
            if (b){
                System.out.println("该年有366天");
            }
            else{
                System.out.println("该年有365天");
            }
        }else{
            if (month == 1 | month == 3 | month == 5 | month == 7 | month == 8 | month == 10 | month == 12){
                System.out.println("该月有31天");
            }else if (month == 2){
                if (b){
                    System.out.println("该月有29天");
                }else{
                    System.out.println("该月有28天");
                }
            }else{
                System.out.println("该月有30天");
            }
        }
    }
    public static ArrayList judge() {
        Scanner sc = new Scanner(System.in);
        ArrayList list = new ArrayList();
        try {
            int year = sc.nextInt();
            int month = sc.nextInt();
            int day = sc.nextInt();
            boolean b =
                    (year % 4 == 0 & year % 400 != 0) | (year % 400 == 0);
            if (year < 0) {
                System.out.print("输入错误!请重新输入:");
                judge();
            } else if (month < 1 | month > 12) {
                System.out.print("输入错误!请重新输入:");
                judge();
            } else if (month == 1 | month == 3 | month == 5 | month == 7 | month == 8 | month == 10 | month == 12) {
                if (day < 1 | day > 31) {
                    System.out.print("输入错误!请重新输入:");
                    judge();
                }
            } else if (month == 2) {
                if (b) {
                    if (day < 1 | day > 29) {
                        System.out.print("输入错误!请重新输入:");
                        judge();
                    }
                } else {
                    if (day < 1 | day > 28) {
                        System.out.print("输入错误!请重新输入:");
                        judge();
                    }
                }
            } else {
                if (day < 1 | day > 30) {
                    System.out.print("输入错误!请重新输入:");
                    judge();
                }
            }
            list.add(year);
            list.add(month);
            list.add(day);
        }catch (InputMismatchException e){
            System.out.print("输入错误!请重新输入:");
            judge();
        }
        return list;
    }
    public static String choose(){
        Scanner sc = new Scanner(System.in);
        String choice = sc.next();
        try {
            if (!choice.equals("年") & !choice.equals("月")){
                System.out.print("输入错误!请重新输入:");
                choose();
            }
        }catch (InputMismatchException e){
            System.out.print("输入错误!请重新输入:");
            choose();
        }
        return choice;
    }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值