Java黑皮书课后题第3章:**3.21(科学:某天是星期几)泽勒一致性...编写程序,提示用户输入年、月、该月的哪一天,显示它是一周中的星期几
生活随笔
收集整理的这篇文章主要介绍了
Java黑皮书课后题第3章:**3.21(科学:某天是星期几)泽勒一致性...编写程序,提示用户输入年、月、该月的哪一天,显示它是一周中的星期几
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
**3.21(科学:某天是星期几)泽勒一致性...编写程序,提示用户输入年、月、该月的哪一天,显示它是一周中的星期几
- 题目
- 题目概述
- 运行示例
- 破题
- 代码
题目
题目概述
**3.21(科学:某天是星期几)泽勒一致性…编写程序,提示用户输入年、月、该月的哪一天,显示它是一周中的星期几
泽勒一致性是由克里斯汀·泽勒开发的用于计算某天是星期几的算法:
h = (q + (26 * (m + 1)) / 10 + k + k / 4 + j / 4 + 5 * j) % 7
其中:
- h是一个星期中的某一天(0为星期六;1为星期天;2为星期一;3为星期二;4为星期三;5为星期四;6为星期五)
- q是某月的第几天
- m是月份(3为三月,4为四月,……,12为十二月),一月和二月分别记为上一年的13和14月
1月转为13,2月转为14,同时年份减1 - j是year / 100。
- k是该世纪的第几年(即year%100)。
运行示例
Enter year: (e.g., 2012): 2015
Enter month: 1-12 : 1
Enter the day of the month:1-31 : 25
Day of the week is Sunday
Enter year: (e.g., 2012): 2012
Enter month: 5
Enter the day of the month:1-31 : 12
Day of the week is Saturday
破题
接收用户输入数据
转换为公式规定要求
代入公式计算
得出结果并转化为str
代码
import java.util.Scanner;public class Test3_21 {public static void main(String[] args) {// 接收用户数据Scanner input = new Scanner(System.in);System.out.println("Enter year: (e.g., 2012): ");int year = input.nextInt();System.out.println("Enter month: 1-12 : ");int m = input.nextInt(); // 需要调整System.out.println("Enter the day of the month:1-31 : ");int q = input.nextInt();// 完善变量的值if(m == 1 || m == 2){m += 12;year -= 1;}int k = year % 100;int j = year / 100;// 代入公式int h = (q + (26 * (m + 1)) / 10 + k + k / 4 + j / 4 + 5 * j) % 7;// 转为对应的英文String str;switch (h){case 0:str = "Saturday";break;case 1:str = "Sunday";break;case 2:str = "Monday";break;case 3:str = "Tuesday";break;case 4:str = "Wednesday";break;case 5:str = "Thursday";break;default:str = "Friday";}System.out.println("Day of the week is " + str);} }总结
以上是生活随笔为你收集整理的Java黑皮书课后题第3章:**3.21(科学:某天是星期几)泽勒一致性...编写程序,提示用户输入年、月、该月的哪一天,显示它是一周中的星期几的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Java黑皮书课后题第3章:*3.20(
- 下一篇: Java黑皮书课后题第3章:**3.22