gpt4 book ai didi

Java 日历对齐问题

转载 作者:行者123 更新时间:2023-12-01 10:21:53 24 4
gpt4 key购买 nike

我正在尝试根据提供的月份和年份用 Java 打印日历。我能够让程序打印该月中正确的天数,并在 7 天结束后打印在日历的下一行,但在该月的第一天是的情况下,我无法让它偏移不是周日。另外,日期的数字应该整齐对齐,但我也无法让我的代码生成。我非常确定我的问题出在 printMonthBody 方法中。我应该在这里使用开关吗?我尝试合并在网上找到的解决方案,但仍然没有运气。欢迎任何和所有建议!谢谢。

import java.util.Scanner;

public class Calendar {

public static void main(String [] args)
{
int m = 0;
int d = 0;
int y = 0;
int startday;

Scanner input = new Scanner( System.in ); // Create a Scanner to obtain user input

System.out.print( "Enter the month (ex. 1 for January, 5 for May): " );
m = input.nextInt();

System.out.print( "Enter the year (ex. 2012, 2020): " );
y = input.nextInt();
System.out.println();

d = getNumDaysInMonth(m ,y);

startday = getStartDay(m, y ,d);
System.out.println( "\t" + startday);

printMonthCalendar( m, d, y );
}

public static void printMonthCalendar( int m, int d, int y)
{
printMonthHeader( m, y );
printMonthBody( m, d, y );
}

public static void printMonthHeader( int m, int y)
{
System.out.println( " " + getMonthName( m ) + " " + getYear( y ));
System.out.println( "---------------------------" );
System.out.println( "Sun Mon Tue Wed Thu Fri Sat");
}

public static void printMonthBody( int m, int d, int y )
{
int i = 0;
int x = 0;

for (i = 1; i <= 7; i++)
{
switch(i) {
case 1: //Mon
System.out.print( " " + i);
x++;
if (x % 7 == 0) {
System.out.println();
}
break;
case 2: //Tue
{
System.out.print( " " + i);
x++;
if (x % 7 == 0) {
System.out.println();
}
}
break;
case 3: //Wed
{
System.out.print( " " + i);
x++;
if (x % 7 == 0) {
System.out.println();
}
}
break;
case 4: //Thu
{
System.out.print( " " + i);
x++;
if (x % 7 == 0) {
System.out.println();
}
}
break;
case 5: //Fri
{
System.out.print( " " + i);
x++;
if (x % 7 == 0) {
System.out.println();
}
}
break;
case 6: //Sat
{
System.out.print( " " + i);
x++;
if (x % 7 == 0) {
System.out.println();
}
}
break;
case 7: //Sun
{
System.out.print( " " + i);
x++;
if (x % 7 == 0) {
System.out.println();
}
}
break;
}
}

}


//use an array to organize months with # of days in each month
public static String getMonthName( int m )
{
String[] month = { " ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"
};
return month[m];
}

public static int getYear( int y )
{ //I added this method to get the year from user
return y;
}

public static int getStartDay( int m, int d, int y )
{ // Adjust month number & year to fit Zeller's numbering system
if ( m < 3 )
{
m = m + 12;
y = y - 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;
}

public static int getNumDaysInMonth( int m, int y )
{
int[] numDays = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

//call is leap year method to calc num of days in feb
if ( ( m == 2 ) && ( isLeapYear(y) == true ) )
numDays[2] = 29;
return numDays[m];
}

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


}

最佳答案

鉴于您知道该月的 startDay,您需要预先填充输出的第一行以偏移该月的第一行,例如...

public static void printMonthBody(int m, int d, int y) {
int i = 0;
int x = 0;

int startDay = -getStartDay(m, d, y);
for (int date = startDay; date < d; date++) {
if (date <= 0) {
System.out.printf("%3s ", "");
} else {
System.out.printf("%3d ", date);
}

x++;
if (x % 7 == 0) {
System.out.println("");
}
}
}

它可以打印出类似...的内容

         Mar 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 30

我强烈、强烈、强烈推荐你看看Formatted Strings ,这将使整个过程变得更加轻松

哦,我不会调试您的 getStartDay 方法,但它给了我错误的结果,2016 年 3 月应该从 星期二 开始。

关于Java 日历对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35545261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com