如何在java中显示日历 [英] How to display calendar in java

查看:135
本文介绍了如何在java中显示日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在进行一个问题集,我必须创建一个显示一年中所有月份的日历,包括其中每月的日期。但是我每个月第一行的间距都有问题。在课堂上我们只学习了switch语句,if,else,while,do-while,for循环

I'm currently doing a problem set where I have to create a calendar displaying all the months of a year, including the days of the month within it. However I'm having issues with the spacing of the first line of each month. In class we have only learned switch statements, if, else, while, do-while, for loops

以下是我当月显示的内容:

Here is what is currently displayed for one of my months:

输出图像
图中没有显示我的输入,但是我写的是当年的2016年,以及当年开始的工作日的5个。

Image of output In the picture is doesn't show my inputs, but what I wrote was 2016 for the year and 5 for the weekday that the year starts in.

输出所需内容的图像
再次,一张所需内容的图片。我认为我的问题可能是我使用的等式:int firstDayEachMonth =(daysMonth + firstDayYear)%7;虽然老师给我们这个等式使用,但似乎它不起作用。

Image of output of what what is desired Again, a picture of what is desired. I think my problem is possibly the equation I used which is: int firstDayEachMonth = (daysMonth + firstDayYear)%7; though the teacher gave this equation to us to use, but it seems like it doesn't work.

正如您所看到的那样,第一行的空格一直向左,它应该与指定的日期对齐,在本例中为1月1月1日星期五和1月2日应该对齐星期六,但目前星期日和星期一。

As you can see the spaces on the first line is all the way to the left, it should be aligned with the designated dates, in this case for January, Jan 1st should align on Friday and Jan 2nd should align with Saturday but it is currently on Sunday and Monday.

import java.util.Scanner;

  public class DisplayCalendar
   {
    public static void main(String[] args)
    {
    //Create a new scanner 
    Scanner input = new Scanner(System.in);

    // Prompt user to enter year 
    System.out.print("Enter a year: ");
    int year = input.nextInt();

    // Prompt user to enter first day of the year
    System.out.print("Enter the weekday that the year starts: ");
    int firstDayYear = input.nextInt();

    // A for loop that prints out each month
    for(int month = 1; month <= 12; month++)
    {
        // Set the value of the amount of days in a month
        int daysMonth = 0;

        // Set value of the month 
        String monthDisplay = "";   

        // Find name of each month and number of days
        switch(month)
        {
            case 1: monthDisplay = "January"; 
                daysMonth = 31;
                break;

            case 2: 
                monthDisplay = "February";
                int leapYear = 0;
                while (leapYear > -1)
                {   
                    // Count all years that are divisible by 4 to be a leap year.
                    leapYear += 4;

                    // If the year inputted is a leap year, the days of the month will be 29.
                    if (year == leapYear)
                    {
                        daysMonth = 29;
                        break;
                    }

                    else 
                    {
                        daysMonth = 28;
                    }
                }
                break;

            case 3: monthDisplay = "March";
                daysMonth = 31;
                break;

            case 4: monthDisplay = "April";
                daysMonth = 30;
                break; 

            case 5: monthDisplay = "May";
                daysMonth = 31;
                break;

            case 6: monthDisplay = "June";
                daysMonth = 30;
                break; 

            case 7: monthDisplay = "July";
                daysMonth = 31;
                break;

            case 8: monthDisplay = "August";
                daysMonth = 31;
                break;

            case 9: monthDisplay = "September";
                daysMonth = 30;
                break;

            case 10: monthDisplay = "October";
                daysMonth = 31;
                break;

            case 11: monthDisplay = "November";
                daysMonth = 30;
                break;

            case 12: monthDisplay = "December";
                daysMonth = 31;
                break; 

            // If the month is not recognized, dialog box will be displayed, and then exits program. 
            default : System.out.print("Invalid: Your month is not recognized. ");
                System.exit(0); 

        }
        // Display the month and year
        System.out.println("                      "+ monthDisplay + " " + year);

        // Display the lines
        System.out.println("_____________________________________");

        // Display the days of the week
        System.out.println("Sun     Mon     Tue     Wed     Thu     Fri     Sat");

        // Print spaces depending on the day the month starts.
        int firstDayEachMonth = (daysMonth + firstDayYear)%7;
        for (int space = 1; space <= firstDayEachMonth; space++)
            System.out.print("   ");

        // Print the days 
        for (int daysDisplay = 1; daysDisplay <= daysMonth; daysDisplay++)
        {
            if (firstDayYear%7 == 0)
                System.out.println();

            System.out.printf("%3d      ", daysDisplay);
            firstDayYear += 1;
        }
        System.out.println();
    }

}

}

推荐答案

你能试试这个例子吗?
我可以看到以下输出:

Can you try this example? I can see the following output:


   February 2016
   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 
package general;

import java.util.Scanner;

公共类DisplayCalendar {

public class DisplayCalendar {

}

这篇关于如何在java中显示日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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