日历打印机 - 如何返回每个新行

时间:2017-02-28 07:10:10

标签: java printing calendar

我正在尝试打印任何一年的全年日历。代码在正确的一天每个月开始,但继续计算七个数字并将下一行返回到相同的起始列。我怎么能纠正这个?

这是我目前的输出:

           2017

          January
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 
          February
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 

并继续这一年。我相信错误在printCalendar方法中,但我不知道该怎么做。 这是我的代码:

import java.util.Scanner;
import java.io.*;

public class CalendarPrinter{

  public static void main(String[]args)throws IOException{
    Scanner kbd = new Scanner(System.in);
    System.out.println("Calendar Printer\n");
    System.out.print("Enter the year? ");
    int year = kbd.nextInt();

    printCalendar(year);

    System.out.println("\n" + year + ".txt generated.");
}

public static void printCalendar(int year) throws IOException {
    PrintWriter outputFile = new PrintWriter(year + ".txt");
    outputFile.println("           " + year);

    for(int month = 1; month <=12; month++) {
        outputFile.println("\n          " + getMonthName(month));
        outputFile.println("Sun Mon Tue Wed Thu Fri Sat");
        int numDays = getNumberofDays(month, year);

        for(int day = 1; day <= numDays; day++){
            int dayWeek = getDayofTheWeek(day, month, year);
            for(int k = 0; k < dayWeek; k++)
                outputFile.print("    ");
            outputFile.printf("%3d ", day);
            if(day % 7 == 0) {
                outputFile.println("");
                dayWeek = 0;
            }
        }
    }
    outputFile.close();
 }

 public static String getMonthName(int month){

    String name;
    switch(month){
    case 1: name = "January";
    break;
    case 2: name = "February";
    break;
    case 3: name = "March";
    break;
    case 4: name = "April";
    break;
    case 5: name = "May";
    break;
    case 6: name = "June";
    break;
    case 7: name = "July";
    break;
    case 8: name = "August";
    break;
    case 9: name = "September";
    break;
    case 10: name = "October";
    break;
    case 11: name = "November";
    break;
    case 12: name = "December";
    break;
    default: name = "Unknown";
    break;
    }
    return name;
  }//method

 public static int getNumberofDays(int month, int year){

    int numDays;
    switch(month){
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        numDays = 31;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        numDays = 30;
        break;
    case 2:
        numDays = isLeap(year)? 29:28;
        break;
    default:
        numDays = 0;
    break;
        }
    return numDays;
  }//method

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


 public static int getDayofTheWeek(int day, int month, int year){

    int y = year - (14-month)/12;
    int x = y+y/4 - y/100 + y/400;
    int m = month + 12 * ((14-month)/12) -2;
    int d = (day + x + (31*m)/12)%7;
    return d;

  }//method

 }//class

1 个答案:

答案 0 :(得分:0)

您使用星期几的方法会弄乱您的打印数据。在这个例子中,我使用一个临时数组来保存一行(a.k.a。一行打印)。一旦将第7天存储在阵列中,我们就打印整周。这也发生在当月的最后一天。

 for (int month = 1; month <= 12; month++) {
        outputFile.println("\n          " + getMonthName(month));
        outputFile.println("Sun Mon Tue Wed Thu Fri Sat");
        int numDays = getNumberofDays(month, year);

        int[] week = new int[7];
        for (int day = 1; day <= numDays; day++) {
            int dayWeek = getDayofTheWeek(day, month, year);
            week[dayWeek] = day;

            // did we reach the 7th day of the week or the last day of the month?
            if ((dayWeek + 1) % 7 == 0 || day == numDays) {
                // print the week as one row
                for (int d : week) {
                    // don't print 0 values
                    outputFile.printf("%3s ", d == 0 ? "" : d);
                }
                outputFile.println("");
                // reset the array to store a new row in the next loop iteration
                week = new int[7];
            }
        }
        outputFile.println("");
    }