回答編集履歴

2

変数名の修正

2015/05/09 09:47

投稿

swordone
swordone

スコア20651

test CHANGED
@@ -146,15 +146,15 @@
146
146
 
147
147
  for(int j = 0; j < MAX_NUMBER; j++) {
148
148
 
149
- //n文字ごとの塊が幾つ目か
149
+ //nごとの塊が幾つ目か
150
150
 
151
- int columns = j / n;
151
+ int lines = j / n;
152
152
 
153
153
 
154
154
 
155
- //行が偶数番目か奇数番目かを判定
155
+ //行の塊が偶数番目か奇数番目かを判定
156
156
 
157
- if(columns % 2 == 0 ){
157
+ if(lines % 2 == 0 ){
158
158
 
159
159
  System.out.println(patternA);
160
160
 

1

別パターン

2015/05/09 09:47

投稿

swordone
swordone

スコア20651

test CHANGED
@@ -73,3 +73,101 @@
73
73
  ```
74
74
 
75
75
  ifで使っている`A^B`は排他的論理和といい,AとBの一方がtrue,他方がfalseの時にtrueを返します.
76
+
77
+ ---
78
+
79
+
80
+
81
+ あるいは,列ごとに表示する文字列のパターンは2通りしかないので,それを先に作ってしまうのも手です.
82
+
83
+ ```lang-java
84
+
85
+ import java.util.Scanner;
86
+
87
+
88
+
89
+ public class Amimemoyou {
90
+
91
+ public static void main (String[] args) {
92
+
93
+ final int MAX_NUMBER = 50;
94
+
95
+ Scanner stdin = new Scanner (System.in);
96
+
97
+
98
+
99
+ System.out.print("Size> ");
100
+
101
+ int n = stdin.nextInt();
102
+
103
+ System.out.print("\n");
104
+
105
+ if(n >= MAX_NUMBER) return;
106
+
107
+
108
+
109
+ StringBuilder lineA = new StringBuilder(),
110
+
111
+ lineB = new StringBuilder();
112
+
113
+ //1行(50文字)分の文字列を作る
114
+
115
+ for(int i = 0; i < MAX_NUMBER; i++) {
116
+
117
+ //n文字ごとの塊で幾つ目か
118
+
119
+ int columns = i / n;
120
+
121
+ if(columns % 2 == 0){
122
+
123
+ lineA.append(" ");
124
+
125
+ lineB.append("*");
126
+
127
+ } else {
128
+
129
+ lineA.append("*");
130
+
131
+ lineB.append(" ");
132
+
133
+ }
134
+
135
+ }
136
+
137
+
138
+
139
+ String patternA = lineA.toString(),
140
+
141
+ patternB = lineB.toString();
142
+
143
+
144
+
145
+ //最大で50行
146
+
147
+ for(int j = 0; j < MAX_NUMBER; j++) {
148
+
149
+ //n文字ごとの塊が幾つ目か
150
+
151
+ int columns = j / n;
152
+
153
+
154
+
155
+ //行が偶数番目か奇数番目かを判定
156
+
157
+ if(columns % 2 == 0 ){
158
+
159
+ System.out.println(patternA);
160
+
161
+ } else {
162
+
163
+ System.out.println(patternB);
164
+
165
+ }
166
+
167
+ }
168
+
169
+ }
170
+
171
+ }
172
+
173
+ ```