欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

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

发布时间:2025/5/22 java 37 豆豆

已经实现的功能:

  • 从键盘输入指定的年、月、日;
  • 计算从2010年1月1日至指定日期共有多少天;
  • 由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除。根据余数判断他是在“打鱼”还是在“晒网”;
  • 输入结果。
  • 源代码如下:

    import java.util.Scanner;public class Date {public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.print("请分别输入指定日期的年、月、日(以空格或换行为界): ");//输入提示语Scanner in = new Scanner(System.in);//键盘输入Count count = new Count(in.nextInt(),in.nextInt(),in.nextInt());//为类Count新建对象countcount.time();count.output();in.close();} }class Count {int year1;//目标年int month1;//目标月int day1;//目标日int number = 0;//初始化目标日期与2010年1月1日相差0天public Count(int year1,int month1,int day1) {//构造函数输入年月日this.year1 = year1;this.month1 = month1;this.day1 = day1;}void time() {//计算目标日期与2010年1月1日相差多少天if(year1>=2010) {for(int year = 2010;year < year1;year++){//年数增加if((year%4 == 0 && year%100 != 0) || year%400 == 0){number += 366;}else{number += 365;}}for(int month = 1;month < month1;month++){//月份增加if(month == 2){if ((year1%4 == 0 && year1%400 != 0) || year1%400 == 0){number += 29;}else {number += 28;}}else if (month == 4 || month == 6 || month == 9 || month == 11){number += 30;}else{number += 31;}}number += (day1-1);//日期增加System.out.println("距离2010年1月1日有 " + number + " 天"); }else System.out.print("");}void output() {//输出指定日期在做什么if(year1>=2010) {int result = number%5;System.out.print("根据三天打鱼两天晒网理论这天在 ");if(result == 0 || result == 1 || result == 2) {System.out.print("打鱼!");}else System.out.print("晒网!");}else System.out.print("对不起,您输入的年份小于2010,请重新输入!");}}

    总结

    以上是生活随笔为你收集整理的Java---中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。