简体   繁体   English

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

[英]Formatting string print outs when making a calendar

My code is below, it doesn't format right, the days if the month are one big line. 我的代码在下面,格式不正确,如果月份是大行,则为几天。 Not how a calendar should be does anyone see formatting errors? 有人会看到日历格式错误吗? As well as can someone explain to me how the leap year checker works, i had to look it up and for the life of me cant figure out how and why it works? 有人可以向我解释the年检查器的工作原理,但我不得不查找它,我一生无法弄清楚它的工作原理和原因? Thanks! 谢谢!

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("");


    }
}

}

For leap year: Added solution leap年:添加解决方案

Didn't understand what you are trying to achieve in for-loop, what exactly is the issue? 不明白您要在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.");
        }
    }

Your problem is in the for loop at the bottom of the class. 您的问题出在类底部的for循环中。 You mean to check the current value of 'i', which you are incrementing when doing the if condition and not the variable 'd', which is not changing on each iteration of the loop. 您的意思是检查当前值'i',在执行if条件时要递增该值,而不是变量'd',该值在循环的每次迭代中都不会改变。

Try this modification: 试试这个修改:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM