一:題目:
中國有句俗語叫“三天打魚兩天曬網”。某人從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;
}
}