Java日历对齐问题

时间:2016-02-22 03:17:42

标签: java

我正在尝试根据提供的月份和年份在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;
  }


}

1 个答案:

答案 0 :(得分:1)

鉴于您知道当月的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月应该从Tuesday开始。