我是靠谱客的博主 英俊铃铛,这篇文章主要介绍三天打鱼两天晒网,现在分享给大家,希望可以做个参考。

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

复制代码
1
2
1.程序风格良好(使用自定义注释模板),提供友好的输入输出。

提高要求:

复制代码
1
2
3
4
1.输入数据的正确性验证。 2.使用文件进行数据测试。如将日期 20100101 20111214 等数据保存在in.txt文件中, 程序读入in.dat文件进行判定,并将结果输出至out.txt文件。

分析问题

复制代码
1
2
3
4
5
6
7
根据题意可以将解题过程分为三步: 1)计算从2010年1月1日开始至指定日期共有多少天; 2)由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除; 3)根据余数判断他是在“打鱼”还是在“晒网”; 若余数为0,1,2,则他是在“打鱼” 否则是在“晒网”

关键问题

复制代码
1
2
起始年到指定日期之间存在闰年和平年天数不一样,我们要一一判断

判断平年和闰年
闰年和平年的判断方法

复制代码
1
2
不是闰年的年份就是平年。

闰年的判断方法:

复制代码
1
2
3
4
5
6
7
8
9
闰年又分为普通闰年和世纪闰年, 普通年判断方法: 能被4整除且不能被100整除的为闰年(如2004年就是闰年,1999年不是闰年)。 世纪年判断方法:能被400整除的是闰年(如2000年是闰年,1900年不是闰年)。 闰年(Leap Year)是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。 补上时间差的年份为闰年。 闰年共有366天 (1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天)。

代码实现分析
首先建立日期的结构体

复制代码
1
2
3
4
5
6
7
private static class date{ int year; int month; int day; }

根据题意要实现文件的输入与输出

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Scanner input = new Scanner(System.in); File inDatFile = new File("F:\in.dat"); File inTxtFile = new File("F:\in.txt"); /* 新建两个文件对象 * inDatFile 作为从in.dat中读取数据 * inTxtFile 作为向in.txt中存储数据 */ Writer inDat = new FileWriter(inDatFile); Writer inTxt = new FileWriter(inTxtFile); //两个Writer对象通过write()函数向对应文件中输入数据 System.out.println("Please enter eight digits year, month and day."); System.out.println("Please input the date begin:"); String beginDate = input.nextLine(); inDat.write(beginDate + "rn"); inTxt.write(beginDate + "rn"); /* 输入流输入的同时向文件中存储 并加上rn使其换行 是为了在读取是一行读取,操作方便 下面的操作相同 */ System.out.println("Please enter eight digits year, month and day."); System.out.println("Please input the date end:"); String endDate = input.nextLine(); inDat.write(endDate + "rn"); inTxt.write(endDate + "rn"); inTxt.close(); inDat.close();

输入数据正确性验证

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
BufferedReader in = new BufferedReader(new FileReader("F:\in.dat"));//打开文件创建数据流 //从in.dat中读取开始日期 strInBeginDate = in.readLine(); inBeginDate = Integer.parseInt(strInBeginDate); if (strInBeginDate.length() != 8) { System.out.println("Date must be 8 digits!"); judge = false; continue; } s = Arrays.copyOf(s,s.length+1); //扩容 如果对内存没有要求可以直接声明一个大容量的数组 s[0] = strInBeginDate.substring(0,8); strInEndDate = in.readLine(); //判断读入长度是否符合年月日规范 if (strInEndDate.length() != 8) { System.out.println("Date must be 8 digits!"); judge = false; continue;//这一个模块是在一个循环中,不符合条件的要重新输入数据,下同 } in.close(); inEndDate = Integer.parseInt(strInEndDate); s = Arrays.copyOf(s,s.length+1); //扩容 如果对内存没有要求可以直接声明一个大容量的数组 s[1] = strInEndDate.substring(0,8); //初始日期大于终止日期,不符合要求重新输入 if (inBeginDate > inEndDate) { System.out.println("Begin date must big than end date"); judge = false; continue;//同上 } //判断日期是否为正确的日期时间 if(!(isRightDate(inBeginDate / 10000,inBeginDate / 100 % 100, inBeginDate % 100) && isRightDate(inEndDate / 10000,inEndDate / 100 % 100,inEndDate % 100))) { System.out.println("The date is wrong"); judge = false; continue; }

下面是isRightDate()函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
private static boolean isRightDate(int year, int month, int day) { int day_tab[][] = {{0,31,28,31,30,31,30,31,31,30,31,30,31}, /*平均每月的天数*/ {0,31,29,31,30,31,30,31,31,30,31,30,31} }; //2019年之后,2019年3月之后还没到,2019年3月2号之后也不符合要求 if (year > 2019 || (year == 2019 && month > 3) || (year == 2019 && month == 3 && day > 2) || (month > 12)) { return false; } int lp = year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 1 : 0; return day_tab[lp][month] >= day;//判断闰年或者平年,并且对应的月份的天数时候超过最大天数 }

计算从开始日期到指定日期的天数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
date today = new date();//指定日期 date term = new date();//从开始日期到指定日期的变量 int sumDays,year,day; //分割字符串,分出年月日 today.year = Integer.parseInt(s[1].substring(0,4)); today.month = Integer.parseInt(s[1].substring(4,6)); today.day = Integer.parseInt(s[1].substring(6,8)); term.month = 12; /*设置变量的初始值:月*/ term.day = 31; /*设置变量的初始值:日*/ for(sumDays = 0, year = 2010; year < today.year; year++) { term.year = year; sumDays += days(term); /*计算从2010年至指定年的前一年共有多少天*/ } sumDays += days(today); /*加上指定年中到指定日期的天数*/ day = sumDays % 5; /*求余数*/ //向out.txt输入结果 File outFile = new File("F:\out.txt"); Writer out = new FileWriter(outFile); if(day >= 0 && day < 3) { System.out.println("he was fishing at that day.n"); out.write("he was fishing at that day.n"); /*打印结果*/ } else { System.out.println("He was sleeping at that day."); out.write("He was sleeping at that day.n"); } out.close();

下面是用到的days()函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
private static int days(date day) { //两个输入代表平年和闰年的天数 int day_tab[][] = {{0,31,28,31,30,31,30,31,31,30,31,30,31}, /*平均每月的天数*/ {0,31,29,31,30,31,30,31,31,30,31,30,31} }; int lp = day.year % 4 == 0 && day.year % 100 != 0 || day.year % 400 == 0 ? 1 : /*判定year为闰年还是平年,lp=0为平年, 1为闰年*/ for(int i = 1; i < day.month; i++) /*计算本年中自1月1日起的天数*/ day.day += day_tab[lp][i]; return day.day; }

到此为止整个程序完成

最后

以上就是英俊铃铛最近收集整理的关于三天打鱼两天晒网的全部内容,更多相关三天打鱼两天晒网内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部