如何在终端上正确打印月份日历 [英] How to Properly print Month Calendar on Terminal

查看:54
本文介绍了如何在终端上正确打印月份日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,在此先感谢您的帮助.我有一个程序,应该根据月和年的用户输入来打印当前月日历.该程序可以正常工作,但是我在格式化方面遇到问题,并且该月的第一天未在适当的日期下开始.

Hello and thanks in advance for the assistance. I have a program that is supposed to print the current month calendar based on the user inputs of month and year. The program mostly work but i am having issues with formatting and the first day of the month is not starting under the proper date.

示例输出:

    October 2020
------------------------------
 Sun Mon Tue Wed Thu Fri Sat
      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 

2020年10月将在星期四开始,但是 1 打印在 Mon 下.10月4日是星期日,因此这是新行开始的地方.

October 2020 will begin on a Thursday, but 1 is printed under Mon. October 4th is a Sunday, so that is where the new line should begin.

请参阅附件我的代码.再次感谢

Please see attached my code. Thanks again

import java.util.*;

public class CalendarMonthDisplay {

        public static void main(String[] args) {
            Scanner input = new Scanner(System.in); //Scan for user input
            System.out.print("Please enter a month between 1 and 12 (e.g. 5): "); //Prompt user to enter month
            int m = input.nextInt();

            System.out.print("Please enter a full year (e.g. 2018): "); //Prompt user to enter year
            int y = input.nextInt();

             //Print calendar for the month of the year
            if ( m < 1 || m > 12)
                System.out.print("Wrong input! Please try again.");
            else
                printMonthCalendar(m, y);
        }

        static void printMonthCalendar (int m, int y) { //Display calendar in format above
            int startDay = getStartDay(m, y);
            int numDaysInMonths = getNumDaysInMonth(m, y);

            printMonthHeader(m, y);
            printMonthBody(startDay, numDaysInMonths);
        }

        static void printMonthBody (int startDay, int numDaysInMonths) { //Display the days in the calendar

            int i;

            for (i = 0; i <= startDay; i++)
                System.out.print(" ");

            for (i = 1; i <= numDaysInMonths; i++) {
                if ( i < 10 )
                    System.out.print("   " + i );
                else
                    System.out.print("  " + i + " ");

                if ((startDay + i) % 7 == 0)
                    System.out.println();
            }
            System.out.println();
        }

        static void printMonthHeader (int m, int y) { //Display the header information
            System.out.println("\t" + getMonthName(m) + " " + y);
            System.out.println("------------------------------");
            System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
        }

        static  String getMonthName (int m) {
            String monthName = null;
            switch (m) {
                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;
        }


        static int getNumDaysInMonth (int m, int y) {
            int numDaysInMonths= 0;
            switch (m) {
                case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                    numDaysInMonths= 31;
                    break;
                case 4: case 6: case 9: case 11:
                    numDaysInMonths = 30;
                    break;
                case 2:
                    if (isLeapYear(y))
                        numDaysInMonths = 29;
                    else
                        numDaysInMonths = 28;
                    break;
            }
            return numDaysInMonths;
        }

        static boolean isLeapYear (int y) {
            return  (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
//                return  true;
//            return false;
        }

        static int getStartDay (int m, int y) {
            // Adjust month number & year to fit Zeller's numbering system
            if (m < 3)
                m = m + 12;
                y = y - 1;

            int d = 1; //Set day parameter to 1
            int k = y % 100;      // Calculate year within century
            int j = y / 100;      // Calculate century term
            int h = 0;            // Day number of first day in month 'm'

            h = ( d + ( 13 * ( m + 1 ) / 5 ) + k + ( k / 4 ) + ( j / 4 ) + ( 5 * j ) ) % 7;

            // Convert Zeller's value to ISO value (1 = Mon, ... , 7 = Sun )
            int dayNum = ( ( h + 5 ) % 7 ) + 1;
            return dayNum;
        }


}

推荐答案

我建议您使用跟踪:日期时间 .

I recommend you do it using the modern date-time API. Learn more about the modern date-time API from Trail: Date Time.

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); // Scan for user input
        System.out.print("Please enter a month between 1 and 12 (e.g. 5): "); // Prompt user to enter month
        int m = input.nextInt();

        System.out.print("Please enter a full year (e.g. 2018): "); // Prompt user to enter year
        int y = input.nextInt();
        printMonth(y, m);
    }

    static void printMonth(int year, int month) {
        YearMonth ym = YearMonth.of(year, month);
        System.out.println("Sun Mon Tue Wed Thu Fri Sat");
        int counter = 1;

        // Get day of week of 1st date of the month and print space for as many days as
        // distant from SUN
        int dayValue = LocalDate.of(year, month, 1).getDayOfWeek().getValue();
        for (int i = 0; i < dayValue; i++, counter++) {
            System.out.printf("%-4s", "");
        }

        for (int i = 1; i <= ym.getMonth().length(ym.isLeapYear()); i++, counter++) {
            System.out.printf("%-4d", i);

            // Break the line if the value of the counter is multiple of 7
            if (counter % 7 == 0) {
                System.out.println();
            }
        }
    }
}

示例运行:

Please enter a month between 1 and 12 (e.g. 5): 9
Please enter a full year (e.g. 2018): 2020
Sun Mon Tue Wed Thu Fri Sat
        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  

注意::有关格式打印的信息,请参见格式化程序.

Note: Learn about formatted printing at Formatter.

这篇关于如何在终端上正确打印月份日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆