一:题目:
中国有句俗语叫“三天打鱼两天晒网”。某人从1990年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。
二:设计思路:
从键盘上输入具体的时间,判断时间是否合法,如果不合法输出输入的时间不合法,如果合法判断是否是闰年计算那天到2010年1月1号有多少天并且除以5看余数是多少,如果余数大于等于一小于等于3则当天在打渔其他时间为晒网
输入格式:
yyyy-mm-dd
代码实现:
import java.util.Scanner;
/**
* Created by Administrator on 2020/5/26.
*/
public class DateMain {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] st = str.split("-");
int y = Integer.valueOf(st[0]);
int m = Integer.valueOf(st[1]);
int d = Integer.valueOf(st[2]);
if (y > 1990 && (m > 0&& m <=12) && d <=31 ) {
jude(getAllDates(y,m,d));
} else {
System.out.print("Invalid input");
}
}
/**
* 判断某天是打鱼还是晒网
*
* @param dates
*/
public static void jude(int dates){
int x = dates % 5;
if (x >=1 && x <=3) {
System.out.print("He is Working");
} else if(x ==4 || x==0){
System.out.print("He is having a rest");
}
}
/**
* 判断是否是闰年
*
* @param year
* @return
*/
public static boolean runNian(int year){
if ((year % 4 ==0 &&year%100 !=0) || year%400 ==0) {
return true;
}
return false;
}
/**
* 获取从1990年到某一年所有的天数
*
* @param year
* @param m
* @param d
* @return
*/
public static int getAllDates(int year,int m,int d){
int sum =0;
for (int i = 1990; i < year;i++) {
if(runNian(i)) {
sum += 366;
} else {
sum += 365;
}
}
sum += getBeforMonth(year, m, d);
return sum;
}
/**
* 获取从1月到某月的天数+该月的天数
*
* @param year
* @param m
* @param d
* @return
*/
public static int getBeforMonth(int year,int m,int d){
int sum = 0;
for (int i= 1; i< m;i++) {
sum += getDays(year,i);
}
return sum + d;
}
/**
* 获取某月的天数
*
* @param year
* @param m
* @return
*/
public static int getDays(int year,int m){
int days = 0;
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days =31;
break;
case 4:
case 6:
case 9:
case 11:
days +=30;
break;
case 2:
if (runNian(year)) {
days = 29;
}else {
days= 28;
}
break;
}
return days;
}
}