思路:
判断年份,月份对应的天数
分别定义变量计算闰年和平年对应天数
用总的天数%5,用switch分支判断输出对应的结果,break防止穿透。
public class DayToFish {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入年份:");
int year = scanner.nextInt();
System.out.println("请输入月份:");
int month = scanner.nextInt();
System.out.println("请输入今天几号:");
int days= scanner.nextInt();
int yearDays=0;
for (int i = 1990; i <year ; i++) {
if(year%4==0&&year%100==0||year%100==0){
yearDays+=365;
} else {
yearDays+=366;
}
}
int monthDays=0;
if(month<=12) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthDays += 31;
break;
case 2:
if (year % 4 == 0 & year % 100 == 0 || year % 100 == 0) {
monthDays += 28;
} else {
monthDays += 29;
}
break;
default:
monthDays += 30;
break;
}
}else {
System.out.println("输入月份不规范");
return;
}
int sum=yearDays+monthDays+days;
if(days<=31) {
switch (sum % 5) {
case 1:
case 2:
case 3:
System.out.println("今天是" + year + "年" + month + "月,第" + days + "日" + "今天该打鱼");
break;
case 4:
case 5:
System.out.println("今天是" + year + "年" + month + "月,第" + days + "日" + "今天该晒网");
break;
}
}else {
System.out.println("输入日期不规范");
return;
}
}
}