zoukankan      html  css  js  c++  java
  • 第六章第三十四题(打印日历)(Print calendar)

    **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;
            else
                leapYear = 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;
                        else
                            day += 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("
               " + monthString + " " + year + "
    ---------------------------------");
                System.out.printf("
    %-5s%-5s%-5s%-5s%-5s%-5s%-5s
    ", "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("
    ");
                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版)更多

  • 相关阅读:
    What is the difference between Serialization and Marshaling?
    IEEE Standard 754 for Binary Floating-Point Arithmetic
    没有单元测试,就很难有真正的积累。
    一般只用 20% 的代码就可以解决 80% 的问题。但要想解决剩下 20% 的问题的话,则需要额外 80% 的代码。
    为失败设计,大量引入对SRE的理解,鲁棒性高
    用git合并分支时,如何保持某些文件不被合并
    git 分支合并时如何忽略某个文件
    Golang拼接字符串的5种方法及其效率_Chrispink-CSDN博客_golang 字符串拼接效率 https://blog.csdn.net/m0_37422289/article/details/103362740
    Lua大量字符串拼接方式效率对比及原因分析
    干货 | 携程多语言平台-Shark系统的高可用演进之路
  • 原文地址:https://www.cnblogs.com/in2013/p/12943109.html
Copyright © 2011-2022 走看看