**6.34(打印日历)编程练习题3.21使用Zeller一致性原理来计算某天是星期几。使用Zeller的算法简化程序清单6-12以获得每月开始的第一天是星期几。

**6.34(Print calendar) Programming Exercise 3.21 uses Zeller’s congruence to calculate the day of the week. Simplify Listing 6.12, PrintCalendar.java, using Zeller’s algorithm to get the start day of the month.
下面是参考答案代码:

// https://cn.fankuiba.com
import java.util.Scanner;public class Ans6_34_page205 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter full year (e.g., 2012): ");int year = input.nextInt();System.out.print("What day is January 1, "+year+" ? ");int week = input.nextInt();int month = 1, day = 0;String monthString = "";boolean leapYear;if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0 && year % 3200 != 0) || year % 172800 == 0)leapYear = true;elseleapYear = false;for (; month <= 12; month++) {switch (month) {case 1:monthString = "January";break;case 2:day += 31;monthString = "February";break;case 3:monthString = "March";if (leapYear)day += 29;elseday += 28;break;case 4:day += 31;monthString = "April";break;case 5:day += 30;monthString = "May";break;case 6:day += 31;monthString = "June";break;case 7:day += 30;monthString = "July";break;case 8:day += 31;monthString = "August";break;case 9:day += 31;monthString = "September";break;case 10:day += 30;monthString = "October";break;case 11:day += 31;monthString = "November";break;case 12:day += 30;monthString = "December";}int days = (week + day) % 7;System.out.print("\n           " + monthString + " " + year + "\n---------------------------------");System.out.printf("\n%-5s%-5s%-5s%-5s%-5s%-5s%-5s\n", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");for (int n =1;n<=days;n++) {System.out.printf("%-5s", "");}int j = 1;if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 ||month == 12) {for (; j <= 31; j++) {System.out.printf("%-5d", j);if ((days+j) % 7 == 0)System.out.println();}}else if (month == 2 && leapYear) {for (; j <= 29; j++) {System.out.printf("%-5d", j);if ((days+j) % 7 == 0)System.out.println();}}else if (month == 2) {for (; j <= 28; j++) {System.out.printf("%-5d", j);if ((days+j) % 7 == 0)System.out.println();}}else {for (; j <= 30; j++) {System.out.printf("%-5d", j);if ((days + j) % 7 == 0)System.out.println();}}System.out.print("\n");switch (days) {case 0:System.out.print("Sun");break;case 1:System.out.print("Mon");break;case 2:System.out.print("Tue");break;case 3:System.out.print("Wed");break;case 4:System.out.print("Thu");break;case 5:System.out.print("Fri");break;case 6:System.out.print("Sat");}System.out.println(" starts on the first day of "+monthString);}}
}

适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版)更多

第六章第三十四题(打印日历)(Print calendar) - 编程练习题答案相关推荐

  1. 第五章第三十五题(加法)(Summation)

    第五章第三十五题(加法)(Summation) *5.35(加法)编写程序,计算下面的和. *5.35(Summation) Write a program to compute the follow ...

  2. 第五章第三十八题(十进制转换八进制)(Decimal to octal)

    第五章第三十八题(十进制转换八进制)(Decimal to octal) **5.38(十进制转换为八进制)编写程序,提示用户输入一个十进制整数,然后显示对应的八进制值.在这个程序中不要使用Java的 ...

  3. 求正多边形的面积JAVA_第六章第三十六题(几何:正多边形的面积)(Geometry: area of a regular polygon)...

    *6.36(几何:正多边形的面积)正多边形是一个n条边的多边形,它的每条边的长度都相等,而且所有角的角度也相等(即多边形既是等边又等角的).计算正多边形面积的公式是: 使用下面的方法头编写方法,返回正 ...

  4. 第三十四题 UVA1589 象棋 Xiangqi 说来惭愧

    PDF 题意翻译 [输入] 输入数据≤40组,对于每组数据,第一行有三个数:第一个数代表红方棋数 N(2≤N≤7),第二三个数代表黑将的坐标 接下来N行每行一个字符两个数,代表每个红子的详细信息,字符 ...

  5. 第七章第三十六题(游戏:八皇后问题)(Game: Eight Queens)

    第七章第三十六题(游戏:八皇后问题)(Game: Eight Queens) ***7.36(游戏:八皇后问题)经典的八皇后难题是要将八个皇后放在棋盘上,任何两个皇后都不能互相攻击(即没有两个皇后是在 ...

  6. 程序员编程艺术第三十四~三十五章:格子取数问题,完美洗牌算法

    第三十四~三十五章:格子取数,完美洗牌算法 作者:July.caopengcs.绿色夹克衫.致谢:西芹_new,陈利人, Peiyush Jain,白石,zinking. 时间:二零一三年八月二十三日 ...

  7. 程序员编程艺术第三十四 三十五章 格子取数问题,完美洗牌算法

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 第三十四 ...

  8. c语言三级上机题库,2006年9月全国等级考试三级c语言上机题库(三十四)

    ★☆题目34(无忧id 73,102 素数题) 无忧id 102 题提供了求素数isPrime()函数 程序prog1.c的功能是:选出100以上1000之内所有个位数字与十位数字之和被10除所得余数 ...

  9. 第三十四章 批量印刷书籍

    第三十四章 批量印刷书籍 "不,这次是要必须跪下去的,因为这是对您的敬重,你竟然能够制作出来如此锋利的宝剑,那么我这个长安城的第一铁匠就当的有些太无能了." "哪里无能, ...

最新文章

  1. 使用linux服务器怎么编译c++
  2. [code] spectral cluster
  3. ubuntu16.04終端補全忽略大小寫
  4. python整形浮点型_Python3基础 | 整型浮点型
  5. nyoj 600——花儿朵朵——【离散化、线段树插线问点】
  6. 06.Android之消息机制问题
  7. 匹配表情emoji 正则_新版Emoji表情过滤
  8. switch语句的ns图怎么画_NS图绘制软件
  9. 定时器/计数器、中断
  10. 移动端安全 | Windows11安卓子系统进行APP抓包(踩坑文)
  11. Raptor 经典例题 2 (冒泡法排序 100名同学拉成一圈)
  12. RESB 0x7dfe-$ 报错
  13. 共模电感适用的频率_共模电感磁芯的选择决定其性能和应用场景——原文转自金昊德官网...
  14. 如何快速掌握HTTP协议?
  15. 跨平台2D游戏引擎CloudBox
  16. 系统架构设计——秒杀系统架构设计
  17. Java程序员最喜欢的五大神器
  18. 磁珠和电感有什么不同?
  19. 有多少爱 就有多少原谅
  20. Python 实现将 Markdown 文档转换为 EPUB 电子书文件

热门文章

  1. 网页抓取软件Wget用法详解
  2. web前端html实例-Html5实现的语音搜索功能
  3. oeasy教您玩转vim - 21 - 状态横条
  4. Error,java对常量池来说字符串xxx的UTF8表示过长
  5. [小o地图-数据] - 城市交通态势数据(实时路况)
  6. 打卡赠书的几点重要说明
  7. oracle系统报表开发工具,掌握七款实用报表开发工具,定制属于自己的报表
  8. HZNU Training 2 for Zhejiang Provincial Collegiate Programming Contest 2019
  9. php notice undefined variable,PHP错误提示,Notice: Undefined variable
  10. MDK解决方案:Warning L6989W