Printing a Calendar from Given Month/Year

后端 未结 4 996
南笙
南笙 2021-01-25 03:36

I\'m working on a Java assignment and it involves printing a calendar after the user specifies a month and a year. I cannot use the Calendar or GregorianCalendar classes. My pro

4条回答
  •  醉梦人生
    2021-01-25 04:04

    I Know you got the answer. But here is quick fix (Sorry its kinda hacky) that you can incorporate to correct your indexes for Saturday without making many changes to your code.

    System.out.println("Su Mo Tu We Th Fr Sa");
            int xx = h == 0 ? 7 : h; // Correct the index for Saturday.
            for (int i = xx; i > 1; i--) // Reversing the loop condition
                System.out.print("   ");
            for (int i = 1; i <= numDays; i++) {
                System.out.printf("%2d ", i);
                if (((i + h - 1) % 7 == 0) || (i == numDays))
                    System.out.println();
            }
    

提交回复
热议问题