“三天打鱼,两天晒网”问题 java求解

这篇博客介绍了如何用Java解决‘三天打鱼两天晒网’的问题。作者首先定义了闰年和平年月份的天数数组,然后通过判断年份是否为闰年来确定每年的天数。接着,根据输入的年、月、日计算出总天数,并依据余数判断是打鱼还是晒网。程序最后展示了用户输入日期后,系统会输出当天的活动情况。
摘要由CSDN通过智能技术生成

“三天打鱼,两天晒网”问题求解

问题描述: 中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。

求解思路

计算某一天是在打鱼还是晒网,关键在于计算这一天距离初始时间2010.1.1 的天数差days。days%5等于1、2、3时,说明这一天在打鱼,否则就在晒网。而不同的年份不同的月份天数都不尽相同,闰年的二月有29天,平年只有28天,而1,3,5,7,8,10,12月有31天,3,6,11月有30天,所以需要区别计算某年某月的具体时间。

程序流程图

流程图

具体代码

package Time;

import java.lang.reflect.Array;
import java.util.Scanner;

public class date {
// 建立俩个月份的数组,分别对应闰年和平年
    private int[] DayOfMonth = {0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    private int[] dayOfMonth = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    // 判断是否为闰年
    public boolean isLeap (int year) {
        if (year % 4 == 0 && year % 100 != 0) {
            return true;
        } else if (year % 400 == 0) {
            return true;
        } else {
            return false;
        }

    }
    // 通过一个计算全年天数的函数优先计算这一年的天数
    public  int daysofyear (int year){
        if (isLeap(year)){
            return 366;
        }
        else return 365;

    }
    // 获取当月天数
    public int getdays(int year,int month){
        if (isLeap(year)){
            return DayOfMonth[month];
        }
        else {
            return dayOfMonth[month];
        }
    }
boolean isreal(){
        return false;
}

    public static void main (String[] args) {
        date A=new date();
        Boolean P=true;
        int days=0;
        int origin=2010;
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入年份");
        int  x = scanner.nextInt();
        System.out.println("请输入月份");
        int y=scanner.nextInt();
        System.out.println("请输入几号");
        int z=scanner.nextInt();

        for (;x<origin;x++){
            days+=A.daysofyear(x);

        }
        for (int i=1;i<y;i++){
            days+= A.getdays(x,i);
        }
        // 对该月的天数做判断,如果输入的天数超过实际,报错。
        if (z>A.dayOfMonth[y]){
            System.out.println("错误,该月没有这么多天!!");
            P= A.isreal();

        }
        else  days+=z;
if (P){
    if (days % 5 == 0 || days % 5 == 4) {
        System.out.println("这天在晒网!");
    } else System.out.println("这天在打鱼!");
}

    }
}

代码调试运行结果

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值