質問編集履歴
3
コード更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -156,7 +156,7 @@
|
|
156
156
|
|
157
157
|
Country[] countries = new Country[1000];
|
158
158
|
|
159
|
-
Country[] gold , silver , bronze;
|
159
|
+
// Country[] gold , silver , bronze;
|
160
160
|
|
161
161
|
|
162
162
|
|
@@ -168,15 +168,15 @@
|
|
168
168
|
|
169
169
|
//country = new String[NUM_OF_COUNTRIES];
|
170
170
|
|
171
|
-
gold = new Country[countries.length];
|
171
|
+
// gold = new Country[countries.length];
|
172
|
-
|
172
|
+
|
173
|
-
silver = new Country[countries.length];
|
173
|
+
// silver = new Country[countries.length];
|
174
|
-
|
174
|
+
|
175
|
-
bronze = new Country[countries.length];
|
175
|
+
// bronze = new Country[countries.length];
|
176
|
+
|
176
|
-
|
177
|
+
//
|
177
|
-
|
178
|
-
|
178
|
+
|
179
|
-
next = 0;
|
179
|
+
// next = 0;
|
180
180
|
|
181
181
|
}
|
182
182
|
|
2
エラー文の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -380,6 +380,18 @@
|
|
380
380
|
|
381
381
|
```
|
382
382
|
|
383
|
+
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
|
384
|
+
|
385
|
+
エラー文
|
386
|
+
|
387
|
+
Exception in thread "main" java.lang.NullPointerException
|
388
|
+
|
389
|
+
at medals1.Ranking.addMedal(Ranking.java:57)
|
390
|
+
|
391
|
+
at medals1.Competition.main(Competition.java:15)
|
392
|
+
|
393
|
+
----------------------------------------------------------------------------------
|
394
|
+
|
383
395
|
Rankingクラスで国名やメダル情報を格納していたメンバ変数を廃止して、代わりにCountryクラスの配列変数countriesを宣言して、これを使用するように書き換えたいです。
|
384
396
|
|
385
397
|
アバウトな説明ですが、アドバイスよろしくお願いします。
|
1
更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -146,16 +146,18 @@
|
|
146
146
|
|
147
147
|
// メンバ変数宣言部分
|
148
148
|
|
149
|
-
// final int NUM_OF_COUNTRIES = 1000;
|
149
|
+
// final int NUM_OF_COUNTRIES = 1000;
|
150
|
-
|
150
|
+
|
151
|
-
// String[] country;
|
151
|
+
// String[] country;
|
152
|
-
|
152
|
+
|
153
|
-
// int[] gold, silver, bronze;
|
153
|
+
// int[] gold, silver, bronze;
|
154
154
|
|
155
155
|
int next;
|
156
156
|
|
157
157
|
Country[] countries = new Country[1000];
|
158
158
|
|
159
|
+
Country[] gold , silver , bronze;
|
160
|
+
|
159
161
|
|
160
162
|
|
161
163
|
|
@@ -164,15 +166,15 @@
|
|
164
166
|
|
165
167
|
public Ranking() {
|
166
168
|
|
167
|
-
//
|
169
|
+
//country = new String[NUM_OF_COUNTRIES];
|
168
|
-
|
170
|
+
|
169
|
-
|
171
|
+
gold = new Country[countries.length];
|
170
|
-
|
172
|
+
|
171
|
-
|
173
|
+
silver = new Country[countries.length];
|
172
|
-
|
174
|
+
|
173
|
-
|
175
|
+
bronze = new Country[countries.length];
|
174
|
-
|
175
|
-
|
176
|
+
|
177
|
+
|
176
178
|
|
177
179
|
next = 0;
|
178
180
|
|
@@ -196,109 +198,109 @@
|
|
196
198
|
|
197
199
|
for (int i = next - 1; i >= 0; i--) {
|
198
200
|
|
199
|
-
if (countries[i].equals(countryName)) {
|
201
|
+
if (countries[i].name.equals(countryName)) {
|
200
202
|
|
201
203
|
return i;
|
202
204
|
|
203
205
|
}
|
204
206
|
|
205
|
-
|
207
|
+
countries[i] = new Country(countryName);
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
return -1;
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
/**
|
220
|
+
|
221
|
+
* IOCコードで指定された国に指定された色のメダルを1つ追加
|
222
|
+
|
223
|
+
* @param countryName 国(3文字のIOCコード)
|
224
|
+
|
225
|
+
* @param color メダルの色
|
226
|
+
|
227
|
+
*/
|
228
|
+
|
229
|
+
public void addMedal(String countryName, Color color) {
|
230
|
+
|
231
|
+
// addMedal前半部分
|
232
|
+
|
233
|
+
int index = indexOfCountry(countryName);
|
234
|
+
|
235
|
+
if (index < 0) {
|
236
|
+
|
237
|
+
countries[next].name = countryName;
|
238
|
+
|
239
|
+
index = next;
|
240
|
+
|
241
|
+
next ++;
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
// addMedal後半部分
|
246
|
+
|
247
|
+
if (color == Color.Gold) {
|
248
|
+
|
249
|
+
countries[index].gold++;
|
250
|
+
|
251
|
+
} else if (color == Color.Silver) {
|
252
|
+
|
253
|
+
countries[index].silver++;
|
254
|
+
|
255
|
+
} else if (color == Color.Bronze) {
|
256
|
+
|
257
|
+
countries[index].bronze++;
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
// sortResultsメソッド部分
|
266
|
+
|
267
|
+
public void sortResults() {
|
268
|
+
|
269
|
+
for (int i = 1; i < countries.length && countries[i] != null; i++) {
|
270
|
+
|
271
|
+
int j = i - 1;
|
272
|
+
|
273
|
+
String c = countries[i].name;
|
274
|
+
|
275
|
+
int g = countries[i].gold;
|
276
|
+
|
277
|
+
int s = countries[i].silver;
|
278
|
+
|
279
|
+
int b = countries[i].bronze;
|
280
|
+
|
281
|
+
while (j >= 0 && c.compareTo(countries[j].name) < 0) {
|
282
|
+
|
283
|
+
countries[j+1].name = countries[j].name;
|
284
|
+
|
285
|
+
countries[j+1].gold = countries[j].gold;
|
286
|
+
|
287
|
+
countries[j+1].silver = countries[j].silver;
|
288
|
+
|
289
|
+
countries[j+1].bronze = countries[j].bronze;
|
290
|
+
|
291
|
+
j--;
|
206
292
|
|
207
293
|
}
|
208
294
|
|
209
|
-
|
210
|
-
|
211
|
-
return -1;
|
212
|
-
|
213
|
-
}
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
/**
|
218
|
-
|
219
|
-
* IOCコードで指定された国に指定された色のメダルを1つ追加
|
220
|
-
|
221
|
-
* @param countryName 国(3文字のIOCコード)
|
222
|
-
|
223
|
-
* @param color メダルの色
|
224
|
-
|
225
|
-
*/
|
226
|
-
|
227
|
-
public void addMedal(String countryName, Color color) {
|
228
|
-
|
229
|
-
// addMedal前半部分
|
230
|
-
|
231
|
-
int index = indexOfCountry(countryName);
|
232
|
-
|
233
|
-
if (index < 0) {
|
234
|
-
|
235
|
-
countries[next] = countryName;
|
236
|
-
|
237
|
-
index = next;
|
238
|
-
|
239
|
-
next ++;
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
// addMedal後半部分
|
244
|
-
|
245
|
-
if (color == Color.Gold) {
|
246
|
-
|
247
|
-
gold[index]++;
|
248
|
-
|
249
|
-
} else if (color == Color.Silver) {
|
250
|
-
|
251
|
-
silver[index]++;
|
252
|
-
|
253
|
-
} else if (color == Color.Bronze) {
|
254
|
-
|
255
|
-
bronze[index]++;
|
256
|
-
|
257
|
-
}
|
258
|
-
|
259
|
-
}
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
// sortResultsメソッド部分
|
264
|
-
|
265
|
-
public void sortResults() {
|
266
|
-
|
267
|
-
for (int i = 1; i < country.length && country[i] != null; i++) {
|
268
|
-
|
269
|
-
int j = i - 1;
|
270
|
-
|
271
|
-
String c = countries[i];
|
272
|
-
|
273
|
-
int g = gold[i];
|
274
|
-
|
275
|
-
int s = silver[i];
|
276
|
-
|
277
|
-
int b = bronze[i];
|
278
|
-
|
279
|
-
while (j >= 0 && c.compareTo(countries[j]) < 0) {
|
280
|
-
|
281
|
-
countries[j+1] = countries[j];
|
282
|
-
|
283
|
-
gold[j+1] = gold[j];
|
284
|
-
|
285
|
-
silver[j+1] = silver[j];
|
286
|
-
|
287
|
-
bronze[j+1] = bronze[j];
|
288
|
-
|
289
|
-
j--;
|
290
|
-
|
291
|
-
}
|
292
|
-
|
293
295
|
j++;
|
294
296
|
|
295
|
-
countries[j] = c;
|
297
|
+
countries[j].name = c;
|
296
|
-
|
297
|
-
|
298
|
+
|
298
|
-
|
299
|
-
silver[j] = s;
|
300
|
-
|
301
|
-
|
299
|
+
countries[j].gold = g;
|
300
|
+
|
301
|
+
countries[j].silver = s;
|
302
|
+
|
303
|
+
countries[j].bronze = b;
|
302
304
|
|
303
305
|
}
|
304
306
|
|
@@ -314,19 +316,19 @@
|
|
314
316
|
|
315
317
|
for (int i = 0; i < countries.length && countries[i] != null; i++) {
|
316
318
|
|
317
|
-
System.out.print(countries[i]);
|
319
|
+
System.out.print(countries[i].name);
|
318
|
-
|
320
|
+
|
319
|
-
int sum = gold
|
321
|
+
int sum = countries[i].gold;
|
320
|
-
|
321
|
-
|
322
|
+
|
322
|
-
|
323
|
-
sum += silver[i];
|
324
|
-
|
325
|
-
System.out.print("," + silver[i]);
|
326
|
-
|
327
|
-
sum += bronze[i];
|
328
|
-
|
329
|
-
System.out.print("
|
323
|
+
System.out.print("(" + countries[i].gold);
|
324
|
+
|
325
|
+
sum += countries[i].silver;
|
326
|
+
|
327
|
+
System.out.print("," + countries[i].silver);
|
328
|
+
|
329
|
+
sum += countries[i].bronze;
|
330
|
+
|
331
|
+
System.out.print("," + countries[i].bronze);
|
330
332
|
|
331
333
|
System.out.println(")[" + sum + "]");
|
332
334
|
|
@@ -342,6 +344,8 @@
|
|
342
344
|
|
343
345
|
|
344
346
|
|
347
|
+
|
348
|
+
|
345
349
|
```
|
346
350
|
|
347
351
|
```
|