Java
“三天打鱼两天晒网”用Java实现:
问题描述
中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。用C或C++语言实现程序解决问题
基本要求:1.程序风格良好(使用自定义注释模板),提供友好的输入输出。
提高要求:1.输入数据的正确性验证。
2.使用文件进行数据测试。如:将日期 20100101 20111214 等数据保存在in.txt文件中,程序读入in.dat文件进行
判定,并将结果输出至out.txt文件。
算法分析
计算从2010年1月1日开始至指定日期共有多少天;由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除;根据余数判断他是在“打鱼”还是在“晒网”;若余数为1,2,3则他是在“打鱼”;若余数为0,4则他是在“晒网”在这三步中,关键是第一步。求从2010年1月1日至指定日期有多少天,要判断经历年份中是否有闰年,二月为29天,平年为28天。闰年的方法可以用伪语句描述如下:如果年份能被4除尽且不能被100除尽或能被400除尽则该年是闰年;否则不是闰年。
流程图
文档读写进行测试
源代码实现
Date.java
package T1;
import java.io.*;
import java.util.Scanner;
/**
* @author Chen
* @params
* @Description //TODO 主函数
* @date 2021/3/22 16:33
* @return
*/
public class Date {
//控制台操作
public static void main(String[] args) {
int y,m,d;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入日期:");
String str = sc.next();
String[] s = str.split("/");
y = Integer.valueOf(s[0]);
m = Integer.valueOf(s[1]);
d = Integer.valueOf(s[2]);
if (y < 2010) {
System.out.println("请输入2010年以后的日期!");
continue;
}
if (m < 0 || m > 12) {
System.out.println("请输入0·12间的月份!!");
continue;
}
if (text2.Pan.runYear(y)) {
if (d > text2.Pan.arr[0][m]){
System.out.println("输入日期错误!");
continue;
}
}
else{
if (d > text2.Pan.arr[1][m]){
System.out.println("输入日期错误!");
continue;
}
}
break;
}
//Date date = new Date(y, m, d);
T1.Pan.runYear(y);
T1.Pan.getAlldays(y,m,d);
T1.Pan.getDays(y,m,d);
T1.Pan.jude(d);
}
/*//文档读写进行数据测试
public static void main(String[] args) throws Exception {
FileReader reader = new FileReader("F:/桌面/in.txt");
BufferedReader br = new BufferedReader(reader);
int date = 0;
String str = null;
while ((str = br.readLine()) != null) {
FileWriter writer=new FileWriter("F:/桌面/out.txt", true);
BufferedWriter bw=new BufferedWriter(writer);
int y, m, d;
String[] s = str.split("/");
//String[] s = str.split("/");
y = Integer.valueOf(s[0]);
m = Integer.valueOf(s[1]);
d = Integer.valueOf(s[2]);
if (y < 2010) {
System.out.println("请输入2010年以后的日期!");
continue;
}
if (m < 0 || m > 12) {
System.out.println("请输入0·12间的月份!!");
continue;
}
if (text2.Pan.runYear(y)) {
if (d > text2.Pan.arr[0][m]){
System.out.println("输入日期错误!");
continue;
}
}
else{
if (d > text2.Pan.arr[1][m]){
System.out.println("输入日期错误!");
continue;
}
}
T1.Pan.runYear(y);
T1.Pan.getAlldays(y,m,d);
T1.Pan.getDays(y,m,d);
bw.write(str +" "+ Pan.jude(d)+"\n");
bw.flush();
bw.close();
}
}*/
}
Pan.java
package T1;
/**
* @author Chen
* @version 1.0
* @description 计算判断
* @date 2021-03-22 18:42
**/
public class Pan {
/**
*
* @params [d]
* @author Chen
* @Description //TODO 判断这一天是打鱼还是晒网
* @date 2021/3/22 22:54
* @return java.lang.String
*/
public static String jude(int d) {
int a = d % 5;
if (a >= 1 && a <= 3) {
return "他去打鱼";
}
else {
return "他在晒网";
}
}
/**
* @return boolean
* @params [y]
* @author Chen
* @Description //TODO 判定y为闰年还是平年,true为闰年,false为平年
* @date 2021/3/22 13:20
*/
public static boolean runYear(int y) {
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
return true;
} else
return false;
}
/**
* @return int
* @params [m, d]
* @author Chen
* @Description //TODO 计算当年天数
* @date 2021/3/22 13:45
*/
public static int getDays(int y, int m, int d) {
int sum = 0;
if (runYear(y)) {
for (int i = 1; i < m; i++) {
sum += arr[0][i];
}
} else {
for (int i = 1; i < m; i++) {
sum += arr[1][i];
}
}
return sum + d;
}
/*平均每月的天数*/
public static int[][] arr = new int[][]{
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,},
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,},
};
/**
* @return int
* @params [y, m, d]
* @author Chen
* @Description //TODO 计算从2010年1月1日到某天的总天数
* @date 2021/3/22 13:27
*/
public static int getAlldays(int y, int m, int d) {
int sum = 0;
for (int i = 2010; i < y; i++) {
if (runYear(i)) {
sum += 366;
} else {
sum += 365;
}
}
sum += getDays(y, m, d);
return sum;
}
}