Java黑皮书课后题第6章:**6.34(打印日历)编程练习题3.21使用Zeller一致性原理来计算某天是星期几,使用Zeller的算法简化程序清单6-12以获得每月开始的第一天是星期几
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第6章:**6.34(打印日历)编程练习题3.21使用Zeller一致性原理来计算某天是星期几,使用Zeller的算法简化程序清单6-12以获得每月开始的第一天是星期几
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
6.34(打印日歷)編程練習題3.21使用Zeller一致性原理來計算某天是星期幾,使用Zeller的算法簡化程序清單6-12以獲得每月開始的第一天是星期幾
- 題目
- 題目描述
- 編程練習題3.21的Zeller一致性
- 程序清單6-12原代碼:英文解釋已被更換為中文
- 本題代碼
題目
題目描述
6.34(打印日歷)編程練習題3.21使用Zeller一致性原理來計算某天是星期幾,使用Zeller的算法簡化程序清單6-12以獲得每月開始的第一天是星期幾
編程練習題3.21的Zeller一致性
澤勒一致性是由克里斯汀·澤勒開發的用于計算某天是星期幾的算法:
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)
程序清單6-12原代碼:英文解釋已被更換為中文
import java.util.Scanner;public class qingdan {public static void main(String[] args) {// 1. 獲取用戶輸入Scanner input = new Scanner(System.in);// 獲取用戶輸入年System.out.print("Enter full year (e.g. , 2012):");int year = input.nextInt();// 獲取用戶輸入月System.out.print("Enter month as a number between 1 and 12:");int month = input.nextInt();// 打印printMonth(year, month);}// 打印日歷public static void printMonth(int year, int month){// 打印表頭printMonthTitle(year, month);// 打印表格內容printMonthBody(year, month);}// 打印表頭的方法public static void printMonthTitle(int year, int month){System.out.println(" " + getMonthName(month) + " " + year);System.out.println("---------------------------");System.out.println(" Sun Mon Tue Wed Thu Fri Sat");}// 獲取月份的英文名public static String getMonthName(int month){String monthName = "";switch (month){case 1: monthName = "January";break;case 2: monthName = "February";break;case 3: monthName = "March";break;case 4: monthName = "April";break;case 5: monthName = "May";break;case 6: monthName = "June";break;case 7: monthName = "July";break;case 8: monthName = "August";break;case 9: monthName = "September";break;case 10: monthName = "October";break;case 11: monthName = "November";break;case 12: monthName = "December";}return monthName;}// 打印表格內容public static void printMonthBody(int year, int month){// 獲取該月第一天是周幾int startDay = getStartDay(year, month);// 獲取當月天數int numberOfDaysInMonths = getNumberOfDaysInMonth(year, month);// 輸出+留空格+換行int i = 0;for (i = 0; i < startDay; i++)System.out.print(" ");for (i = 1;i <= numberOfDaysInMonths;i++){System.out.printf("%4d", i);if ((i+startDay) % 7 == 0)System.out.println();}System.out.println();}// 獲取每月第一天是周幾public static int getStartDay(int year, int month){final int START_DAY_FOR_JAN_1_1800 = 3;// 獲取從1970年1月1日到這個月1號有多少天(原文:日本1800年1月1日到本月1日)int totalNumberOfDays = getTotalNumberOfDays(year, month);// 返回每月1號是周幾return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;}// 獲取從(日本1800年1月1日)到今年的總天數public static int getTotalNumberOfDays(int year, int month){int total = 0;// 獲取總天數(以年為單位計算)for (int i = 1800; i < year;i++){if (isLeapYear(i))total += 366;elsetotal += 365;}// 獲取總天數(以月為單位計算)for (int i = 1;i < month ; i++)total += getNumberOfDaysInMonth(year, i);return total;}// 獲取一個月的天數public static int getNumberOfDaysInMonth(int year, int month){if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8|| month == 10 || month == 12)return 31;if (month == 4 || month == 6 || month == 9 || month == 11)return 30;if (month == 2) return isLeapYear(year) ? 29:28;return 0; // 當月份數有誤時返回0}// 判斷是否是閏年public static boolean isLeapYear(int year){return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);} }本題代碼
import java.util.Scanner;public class Test6_34 {public static void main(String[] args) {// 1. 獲取用戶輸入Scanner input = new Scanner(System.in);// 獲取用戶輸入年System.out.print("Enter full year (e.g. , 2012):");int year = input.nextInt();// 獲取用戶輸入月System.out.print("Enter month as a number between 1 and 12:");int month = input.nextInt();// 打印printMonth(year, month);}// 打印日歷public static void printMonth(int year, int month){// 打印表頭printMonthTitle(year, month);// 打印表格內容printMonthBody(year, month);}// 打印表頭的方法public static void printMonthTitle(int year, int month){System.out.println(" " + getMonthName(month) + " " + year);System.out.println("---------------------------");System.out.println(" Sun Mon Tue Wed Thu Fri Sat");}// 獲取月份的英文名public static String getMonthName(int month){String monthName = "";switch (month){case 1: monthName = "January";break;case 2: monthName = "February";break;case 3: monthName = "March";break;case 4: monthName = "April";break;case 5: monthName = "May";break;case 6: monthName = "June";break;case 7: monthName = "July";break;case 8: monthName = "August";break;case 9: monthName = "September";break;case 10: monthName = "October";break;case 11: monthName = "November";break;case 12: monthName = "December";}return monthName;}// 打印表格內容public static void printMonthBody(int year, int month){// 獲取該月第一天是周幾//int startDay = getStartDay(year, month);// 獲取當月天數int numberOfDaysInMonths = getNumberOfDaysInMonth(year, month);//轉為公式內因子int q = 1;int m = month;if (month == 1 || month == 2){month += 12;year -= 1;}int k = year % 100;int j = year / 100;int startDay = (q + (26 * (m + 1)) / 10 + k + k / 4 + j / 4 + 5 * j) % 7;startDay = (startDay + 6) % 7;// 輸出+留空格+換行int i = 0;for (i = 0; i < startDay; i++)System.out.print(" ");for (i = 1;i <= numberOfDaysInMonths;i++){System.out.printf("%4d", i);if ((i+startDay) % 7 == 0)System.out.println();}System.out.println();}// 獲取一個月的天數public static int getNumberOfDaysInMonth(int year, int month){if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8|| month == 10 || month == 12)return 31;if (month == 4 || month == 6 || month == 9 || month == 11)return 30;if (month == 2) return isLeapYear(year) ? 29:28;return 0; // 當月份數有誤時返回0}// 判斷是否是閏年public static boolean isLeapYear(int year){return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第6章:**6.34(打印日历)编程练习题3.21使用Zeller一致性原理来计算某天是星期几,使用Zeller的算法简化程序清单6-12以获得每月开始的第一天是星期几的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第6章:**6.31
- 下一篇: Java黑皮书课后题第6章:*6.36(