回答編集履歴
1
コードの改善
test
CHANGED
@@ -16,15 +16,15 @@
|
|
16
16
|
|
17
17
|
質問への回答とは別に、
|
18
18
|
|
19
|
-
|
19
|
+
2次元配列と `java.util.Calendar` を使わないコードを書いてみました。
|
20
20
|
|
21
21
|
```java
|
22
22
|
|
23
23
|
class Main {
|
24
24
|
|
25
|
-
static final int
|
25
|
+
static final int[] a = { 31,28,31,30,31,30,31,31,30,31,30,31 };
|
26
26
|
|
27
|
-
static final int
|
27
|
+
static final int[] b = { 1, 4, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
|
28
28
|
|
29
29
|
|
30
30
|
|
@@ -40,19 +40,19 @@
|
|
40
40
|
|
41
41
|
int m = s.nextInt();
|
42
42
|
|
43
|
-
int
|
43
|
+
int z = a[m-1]; // z は m月の最終日
|
44
44
|
|
45
|
-
i
|
45
|
+
if (m == 2 && y%4 == 0 && (y%100 != 0 || y%400 == 0)) z++; // 閏年
|
46
46
|
|
47
|
-
i
|
47
|
+
if (m < 3) y--; // 3月より前の月は y年の閏年の影響を受けない
|
48
48
|
|
49
|
-
i
|
49
|
+
int w = (y + y/4 - y/100 + y/400 + b[m-1]) % 7; // w は曜日
|
50
50
|
|
51
|
-
System.out.print("日 月 火 水 木 金 土\n" + " ".repeat(w
|
51
|
+
System.out.print("日 月 火 水 木 金 土\n" + " ".repeat(w*3));
|
52
52
|
|
53
53
|
for (int d = 1; d <= z; d++)
|
54
54
|
|
55
|
-
System.out.printf("%2d%
|
55
|
+
System.out.printf("%2d%s", d, ++w%7 == 0 ? "\n" : " ");
|
56
56
|
|
57
57
|
for (; w < 42; w += 7) System.out.println();
|
58
58
|
|