在制作日历时格式化字符串打印输出

时间:2016-02-17 04:23:05

标签: java format-string

我的代码在下面,它没有正确的格式,如果月份是一个大线的日子。不是日历应该如何看待格式错误?除了有人可以向我解释闰年检查器是如何工作的,我不得不查看它,因为我的生活无法弄清楚它是如何以及为什么有效的?谢谢!

package calendaryear;
import java.util.Scanner;
public class CalendarYear {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String month;
    String day;
    Scanner userinput = new Scanner(System.in);
    //enter the day!
    System.out.print("Enter day [0 is for Sunday, 1 is for moneday continued]:");
    int d = userinput.nextInt();
    //enter a year!
    System.out.print("Enter a year:");
    int y = userinput.nextInt();
    // for loop for 12 months!
    for (int m = 1; m <= 12; m++){
        switch (m) {
            case 1:
                month = "January";
                break;
            case 2:
                month = "February";
                break;
            case 3:
                month = "March";
                break;
            case 4:
                month = "April";
                break;
            case 5:
                month = "May";
                break;
            case 6:
                month = "June";
                break;
            case 7:
                month = "July";
                break;
            case 8:
                month = "August";
                break;
            case 9:
                month = "September";
                break;
            case 10:
                month = "October";
                break;
            case 11:
                month = "November";
                break;
            case 12:
                month = "December";
                break;
            default:
                month ="";
                }

        System.out.printf("%" + (34 + month.length()) / 2 + "s %s\n", month, y);
        System.out.println(String.format("%39S", "").replace(' ', '-'));
        System.out.printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s\n", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

        int l = 30 + ((m + (int)(m / 8.0)) % 2);
        if (m == 2) {
            if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)){
                l = 29;
            }else {
                l = 28;
            }
        }
        d = (d) % 7;
        switch (d) {
            case 0:
                day = "Sunday";
                break;
            case 1:
                day = "Moneday";
                break;
            case 2:
                day = "Tuesday";
                break;
            case 3:
                day = "Wednesday";
                break;
            case 4:
                day = "Thursday";
                break;
            case 5:
                day = "Friday";
                break;
            case 6:
                day = "Saturday";
                break;
            default:
                day = "";
        }
        if (d != 0) {
            System.out.print(String.format("%" + 6 * d + "s", ""));

        }

        for (int i = 1; i<= l; i++){
            System.out.printf("%-6d", i);
            if (d % 7 == 6){
                System.out.print("");
            }
            day += 1;





        }
        System.out.print("");


    }
}

}

2 个答案:

答案 0 :(得分:0)

对于闰年:添加解决方案

  

不明白你想要在for循环中实现什么,究竟是什么问题?

 public void leapYear(int year)
    {
        System.out.println("This program calculates leap year.");
         if ((year % 4 == 0) && year % 100 != 0)
        {
            System.out.println(year + " is a leap year.");
        }
        else if (year % 400 == 0)
        {
            System.out.println(year + " is a leap year.");
        }
        else
        {
            System.out.println(year + " is not a leap year.");
        }
    }

答案 1 :(得分:0)

您的问题出在课程底部的for循环中。你的意思是检查'i'的当前值,你在执行if条件时递增,而不是变量'd',它在循环的每次迭代中都不会改变。

尝试此修改:

       for (int i = 1; i<= l; i++){
            System.out.printf("%-6d", i);
            if (i % 7 == 6){
                System.out.println();
            }
            day += 1;
        }