久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從給定的月份/年份打印日歷

Printing a Calendar from Given Month/Year(從給定的月份/年份打印日歷)
本文介紹了從給定的月份/年份打印日歷的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我正在處理一項 Java 作業(yè),它涉及在用戶指定月份和年份后打印日歷.我不能使用 Calendar 或 GregorianCalendar 類.我的問題是日歷在星期六的第一天沒有正確打印月份.我已經(jīng)查看了我的代碼大約一個小時,但我不確定出了什么問題.我正在使用 Zeller's Congruence 來查找該月的第一天,用h"表示.

I'm working on a Java assignment and it involves printing a calendar after the user specifies a month and a year. I cannot use the Calendar or GregorianCalendar classes. My problem is that the calendar does not correctly print months with their first day on a Saturday. I've looked at my code for about an hour now, and I'm not sure what went wrong. I'm using Zeller's Congruence to find the first day of the month, which is represented by "h".

例如,2008 年 3 月的日歷(錯誤地)如下所示:

For example, the calendar on March 2008 (incorrectly) looks like this:

     March 2008
Su Mo Tu We Th Fr Sa
 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 

這是我的代碼:

package calendar;

import java.util.Scanner;

public class Calendar
{
    private static int numDays = 0;
    private static int h = 0;
    public static boolean leap(int year)
    {
        if(((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static void firstDayOfYear(int year)
    {
        int month = 13;
        year--;
        h = (1 + (int)(((month + 1) * 26) / 10.0) + year + (int)(year / 4.0) + 6 * (int)(year / 100.0) + (int)(year / 400.0)) % 7;
        String dayName = "";
        switch(h)
        {
            case 0: dayName = "Saturday"; break;
            case 1: dayName = "Sunday"; break;
            case 2: dayName = "Monday"; break;
            case 3: dayName = "Tuesday"; break;
            case 4: dayName = "Wednesday"; break;
            case 5: dayName = "Thursday"; break;
            default: dayName = "Friday"; break;
        }
        System.out.println("The first day of the year is " + dayName);
    }
    public static void firstDayOfMonth(int month, int year)
    {
        if(month == 1 || month == 2)
        {
            month += 12;
            year--;
        }
        h = (1 + (int)(((month + 1) * 26) / 10.0) + year + (int)(year / 4.0) + 6 * (int)(year / 100.0) + (int)(year / 400.0)) % 7;
        String dayName = "";
        switch(h)
        {
            case 0: dayName = "Saturday"; break;
            case 1: dayName = "Sunday"; break;
            case 2: dayName = "Monday"; break;
            case 3: dayName = "Tuesday"; break;
            case 4: dayName = "Wednesday"; break;
            case 5: dayName = "Thursday"; break;
            default: dayName = "Friday"; break;
        }
        System.out.println("The first day of the month is " + dayName);
    }
    public static void numDaysInMonth(int month, int year)
    {
        int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        if (month == 2 && leap(year)) days[month] = 29;
        numDays = days[month];
        System.out.println("The number of days in the month is " + numDays);
    }
    public static void printCal(int month, int year)
    {
        String[] monthNames = {"","January","February","March","April","May","June","July","August","September","October","November","December"};

        System.out.println("    " + monthNames[month] + " " + year);
        System.out.println("Su Mo Tu We Th Fr Sa");
        for (int i = 0; i < h - 1; i++)
            System.out.print("   ");
        for (int i = 1; i <= numDays; i++)
        {
            System.out.printf("%2d ", i);
            if (((i + h - 1) % 7 == 0) || (i == numDays)) System.out.println();
        }
    }
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter month (1-12): ");
        int month = input.nextInt();
        if(month < 1 || month > 12)
        {
            System.out.println("Invalid month. Valids inputs are 1-12.");
            System.exit(0);
        }
        System.out.print("Enter year: ");
        int year = input.nextInt();
        if(year < 1753)
        {
            System.out.println("Invalid year. Valid inputs are 1753 and beyond.");
            System.exit(0);
        }
        if(leap(year))
        {
            System.out.println(year + " is a leap year.");
        }
        else
        {
            System.out.println(year + " is NOT a leap year.");
        }
        firstDayOfYear(year);
        firstDayOfMonth(month, year);
        numDaysInMonth(month, year);
        printCal(month, year);
    }    
}

推薦答案

從星期六開始的每個月.這意味著問題可能出在這一行 -

Your code will have the same issue for every month that starts with a Saturday. This means that the problem is probably in this line -

for (int i = 0; i < h - 1; i++)
  System.out.print("   ");

在這里將 h 設(shè)為 7 而不是 0 將為您解決此問題.您可以在此處修復(fù)該問題,或者您可能需要從 1 到 7 而不是 0 到 6 開始 h 并進行其他所需的更改.

Having h as 7 instead of 0 here will fix it for you. You can either fix that here or you may need to start h from 1 to 7 instead of 0 to 6 and make other required changes of course.

這篇關(guān)于從給定的月份/年份打印日歷的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 中文字幕第一页在线 | 99精彩视频 | 中文字幕精品一区二区三区精品 | 久久手机在线视频 | 天天躁日日躁xxxxaaaa | 综合色婷婷 | 亚洲色图50p| 在线免费观看成人 | 亚洲一区欧美 | 精品国产一区二区三区久久狼黑人 | av在线一区二区三区 | 久在线 | 久久精品免费一区二区三 | 久久久久国产精品午夜一区 | 日韩精品在线视频免费观看 | 涩涩操 | 欧美精品一区二区在线观看 | 欧美精品中文字幕久久二区 | 999热精品 | 欧美一级黄色免费看 | 国产成人精品视频在线观看 | 亚洲成人久久久 | 国产韩国精品一区二区三区 | 中国一级特黄毛片大片 | 精品乱码一区二区三四区 | 精品国产一二三区 | 91天堂网 | 91麻豆精品国产91久久久久久久久 | av毛片| 日韩欧美操 | 亚洲国产精品一区二区三区 | 亚洲精品一区二区网址 | 国产免费一区二区 | 女同久久另类99精品国产 | 午夜影院毛片 | 欧美精品乱码99久久影院 | 免费在线精品视频 | 久日精品 | 亚洲精品中文字幕在线 | 日日欧美 | 精品免费在线 |