我是靠谱客的博主 秀丽可乐,这篇文章主要介绍用java的循环实现出,三天打鱼,两天晒网问题,现在分享给大家,希望可以做个参考。

某人从1990年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是在“打鱼”,还是在“晒网”。

复制代码
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.Scanner; public class Homework4 { 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 day = scanner.nextInt(); judge(getAllDays(year, month, day)); } // 判断打鱼还是晒网 public static void judge(int days) { int x = days % 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 % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return true; } return false; } // 计算距离输入日期多少天 public static int getAllDays(int year, int month, int day) { int sum = 0; // 计算1990--(year-1)之间有多少天 for (int i = 1990; i < year; i++) { if (runNian(i)) { sum += 366; } else { sum += 365; } } // 计算year内多少天 sum += getBeforeDays(year, month, day); return sum; } // 判断每月有几天 public static int getDays(int year, int month) { int days = 0; switch (month) { 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; } // 计算本年内该日期之前共有多少天 public static int getBeforeDays(int year, int month, int day) { int sum = 0; for (int i = 1; i < month; i++) { sum += getDays(year, i); } return sum + day; } }

最后

以上就是秀丽可乐最近收集整理的关于用java的循环实现出,三天打鱼,两天晒网问题的全部内容,更多相关用java内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部