内容:
中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。
步骤:
1.算法分析根据题意可以将解题过程分为三步:
1)计算从2010年1月1日与指定日期相差多少天;
2)由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除;
3)根据余数判断他是在“打鱼”还是在“晒网”;
若 余数为0,1,2,则他是在“打鱼”
否则 是在“晒网”
求从2010年1月1日与指定日期相差多少天,要判断经历年份中是否有闰年,二月为29天,平年为28天。闰年的方法可以用伪语句描述如下:
如果 ((年能被4除尽 且 不能被100除尽)或 能被400除尽)
则 该年是闰年;
否则 不是闰年。
2.概要设计
判断年份是否正确;
判断月份是否正确;
判断日期是否正确;
判断打鱼还是晒网;
判断是否是闰年;
计算距离输入日期多少天;
计算2010-(year-1)之间有多少天;
计算本年内该日期之前共有多少天;
判断每月有几天;
计算本年内该日期之前共有多少天。
3.测试
4.源代码
package cxsjjc;
import java.util.Scanner;
public class Fish
{
public static void main (String[] args)
{
boolean flag = false;
flag=true;
while(flag)
{
Scanner scanner = new Scanner(System.in);
System.out.println(“输入年:”);
int year = scanner.nextInt();
if (year<2010)
{
System.out.println(“错误,请重新输入年”);
year = scanner.nextInt();
}
System.out.println(“输入月:”);
int month = scanner.nextInt();
if ( month < 1 | month > 12)
{
System.out.println(“错误,请重新输入月”);
month = scanner.nextInt();
}
System.out.println(“请输入日”);
int day = scanner.nextInt();
if (day > 31 | day < 1 )
{
System.out.println(“错误,请重新输入日”);
day = scanner.nextInt();
}
judge(getAllDays(year, month, day));
}
}
private static int getAllDays(int year, int month, int day) {
// TODO Auto-generated method stub
return 0;
}
//打鱼晒网
public static void judge(int day) {
int x = day % 5;
if (x >= 1 && x <= 3) {
System.out.println(“打鱼”);
} else if (x == 4 || x == 0) {
System.out.println(“晒网!”);
}
}
//闰年
public static boolean RunNian(int year) {
if ((year%40&&year%100!=0)||year%4000) {
return true;
}
return false;
}
}