Java选择日历闰年

时间:2015-10-02 04:03:27

标签: java boolean selection

如何让java将选择应用于闰年?规则是每4年有一个从0开始的闰年。我做的布尔值是leapYear = year%4 == 0但是当我输入leapYear时,例如4,if语句(3月)用leapYear输出空行。这是代码:

        public static void main(String[] args) {
// create a scanner
Scanner input = new Scanner(System.in);

//prompt user to enter year and first weekday
System.out.println("Enter a year");
int year = input.nextInt();
System.out.println("Enter first weekday of year e.g.: 0 for Sunday");
int weekDay = input.nextInt();
int i;
int jan = 31;
int feb = 28;
int mar = 31;
int apr = 30;
int may = 31;
int jun = 30;
int jul = 31;
int aug = 31;
int sep = 30;
int oct = 31;
int nov = 30;
int dec = 31;
// leap year
boolean leapYear = year % 4 == 0;
if (leapYear) feb += 1;

System.out.println("January " + year);
System.out.println("________________________________");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

if (weekDay == 1) System.out.printf("%4s", " ");
if (weekDay == 2) System.out.printf("%8s", " ");
if (weekDay == 3) System.out.printf("%12s", " ");
if (weekDay == 4) System.out.printf("%16s", " ");
if (weekDay == 5) System.out.printf("%20s", " ");
if (weekDay == 6) System.out.printf("%24s", " ");

for (i = 1; i <= jan; i++) {
    System.out.printf("%4d", i);
    if (weekDay == 0 && i % 7 == 0) System.out.println("\n");
    if (weekDay == 1 && (i == 6 || i == 13 || i == 20 || i == 27)) System.out.println("\n");
    if (weekDay == 2 && (i == 5 || i == 12 || i == 19 || i == 26)) System.out.println("\n");
    if (weekDay == 3 && (i == 4 || i == 11 || i == 18 || i == 25)) System.out.println("\n");
    if (weekDay == 4 && (i == 3 || i == 10 || i == 17 || i == 24)) System.out.println("\n");
    if (weekDay == 5 && (i == 2 || i == 9 || i == 16 || i == 23 || i == 30)) System.out.println("\n");
    if (weekDay == 6 && (i == 1 || i == 8 || i == 15 || i == 22 || i == 29)) System.out.println("\n");
    }
System.out.println("\n");
System.out.println("February " + year);
System.out.println("________________________________");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
if (weekDay == 0) System.out.printf("%12s", " ");
if (weekDay == 1) System.out.printf("%16s", " ");
if (weekDay == 2) System.out.printf("%20s", " ");
if (weekDay == 3) System.out.printf("%24s", " ");
if (weekDay == 5) System.out.printf("%4s", " ");
if (weekDay == 6) System.out.printf("%8s", " ");
for (i = 1; i <= feb; i++) {
    System.out.printf("%4d", i);
    if (weekDay == 0 && (i == 4 || i == 11 || i == 18 || i == 25)) System.out.println("\n");
    if (weekDay == 1 && (i == 3 || i == 10 || i == 17 || i == 24)) System.out.println("\n");
    if (weekDay == 2 && (i == 2 || i == 9 || i == 16 || i == 23)) System.out.println("\n");
    if (weekDay == 3 && (i == 1 || i == 8 || i == 15 || i == 22)) System.out.println("\n");
    if (weekDay == 4 && i % 7 == 0) System.out.println("\n");
    if (weekDay == 5 && (i == 6 || i == 13 || i == 20 || i == 27)) System.out.println("\n");
    if (weekDay == 6 && (i == 5 || i == 12 || i == 19 || i == 26)) System.out.println("\n");

}

System.out.println("\n");
System.out.println("March " + year);
System.out.println("________________________________");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

if (leapYear) {
    if (weekDay == 0) System.out.printf("%4s", " ");
    if (weekDay == 1) System.out.printf("%8s", " ");
    if (weekDay == 2) System.out.printf("%12s", " ");
    if (weekDay == 3) System.out.printf("%16s", " ");
    if (weekDay == 5) System.out.printf("%20s", " ");
    if (weekDay == 6) System.out.printf("%24s", " ");

    for (i = 1; i <= mar; i++) {
        if (weekDay == 6 && (i == 4 || i == 11 || i == 18 || i == 25)) System.out.println("\n");
        if (weekDay == 0 && (i == 3 || i == 10 || i == 17 || i == 24)) System.out.println("\n");
        if (weekDay == 1 &&(i == 2 || i == 9 || i == 16 || i == 23 || i == 30)) System.out.println("\n");
        if (weekDay == 2 &&(i == 1 || i == 8 || i == 15 || i == 22 || i == 29)) System.out.println("\n");
        if (weekDay == 3 && i % 7 == 0) System.out.println("\n");
        if (weekDay == 4 && (i == 6 || i == 13 || i == 20 || i == 27)) System.out.println("\n");
        if (weekDay == 5 && (i == 5 || i == 12 || i == 19 || i == 26)) System.out.println("\n"); 
        }
}
else {
if (weekDay == 0) System.out.printf("%12s", " ");
if (weekDay == 1) System.out.printf("%16s", " ");
if (weekDay == 2) System.out.printf("%20s", " ");
if (weekDay == 3) System.out.printf("%24s", " ");
if (weekDay == 5) System.out.printf("%4s", " ");
if (weekDay == 6) System.out.printf("%8s", " ");

for (i = 1; i <= mar; i++) {
    System.out.printf("%4d", i);
    if (weekDay == 0 && (i == 4 || i == 11 || i == 18 || i == 25)) System.out.println("\n");
    if (weekDay == 1 && (i == 3 || i == 10 || i == 17 || i == 24)) System.out.println("\n");
    if (weekDay == 2 && (i == 2 || i == 9 || i == 16 || i == 23 || i == 30)) System.out.println("\n");
    if (weekDay == 3 && (i == 1 || i == 8 || i == 15 || i == 22 || i == 29)) System.out.println("\n");
    if (weekDay == 4 && i % 7 == 0) System.out.println("\n");
    if (weekDay == 5 && (i == 6 || i == 13 || i == 20 || i == 27)) System.out.println("\n");
    if (weekDay == 6 && (i == 5 || i == 12 || i == 19 || i == 26)) System.out.println("\n");    
}
}

1 个答案:

答案 0 :(得分:1)

您的代码难以阅读,但您缺少System.out.printf(“%4d”,i);在if(leapYear)下的块中。但是,你也应用闰年到三月......

另一种方法......

import java.util.Scanner;

public class StackOverflow_32900399 {

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        try
        {
            System.out.println("Enter a year");
            int year = input.nextInt();

            System.out.println("Enter first weekday of year e.g.: 0 for Sunday");
            int weekDay = input.nextInt();

            for (int month = 1; month <= 12; month++)
            {
                printMonthHeader(month, year);
                weekDay = printDaysOfMonth(month, year, weekDay);
                System.out.println("\n");
            }
        }
        finally
        {
            input.close();
        }
    }

    private static void printMonthHeader(int monthOfYear, int year)
    {       
        String month = "";

        switch (monthOfYear)
        {
            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; 
        }


        System.out.println(month + " " + year);
        System.out.println("___________________________________");
        System.out.println(" Sun  Mon  Tue  Wed  Thu  Fri  Sat ");
    }

    private static int printDaysOfMonth(int monthOfYear, int year, int startOn)
    {
        int daysInMonth = 0;
        int currentDay = startOn;

        switch(monthOfYear)
        {
            case 4 : 
            case 6 : 
            case 9 : 
            case 11 : 
                daysInMonth = 30;
                break;
            case 2:
                daysInMonth = ((year % 4) == 0 ? 29 : 28);
                break;
            default: 
                daysInMonth = 31;
        }

        StringBuilder buff = new StringBuilder();
        if (currentDay != 0)
        {
            buff.append(String.format("%1$"+(5*startOn)+ "s", ""));
        }

        for (int day = 1; day <= daysInMonth; day ++)
        {
            String padding = day > 9 ? "  " : "   ";
            buff.append(padding + day + " ");

            if (currentDay == 6)
            {
                buff.append("\n");
                currentDay = 0;
            }
            else
            {
                currentDay++;
            }
        }

        System.out.println(buff.toString());
        System.out.println("___________________________________");
        return currentDay;
    }

}