回答編集履歴

1

変更

2016/04/28 04:15

投稿

root_jp
root_jp

スコア4666

test CHANGED
@@ -1,81 +1,75 @@
1
1
  ```java
2
2
 
3
- String name[] = { "田中", "加藤", "森田", "北国" };
4
-
5
- String test[] = { "国語", "英語", "理科", "社会" };
3
+ public static void main(String[] args) throws Exception {
6
4
 
7
5
 
8
6
 
9
- int point[][] = { { 100, 80, 50, 30 }, { 30, 40, 70, 90 }, { 50, 90, 100, 0 }, { 0, 10, 70, 30 } };
7
+ String name[] = { "田中", "加藤", "森田", "北国" };
8
+
9
+ String test[] = { "国語", "英語", "理科", "社会" };
10
10
 
11
11
 
12
12
 
13
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13
+ int point[][] = { { 100, 80, 50, 30 }, { 30, 40, 70, 90 }, { 50, 90, 100, 0 }, { 0, 10, 70, 30 } };
14
14
 
15
15
 
16
16
 
17
- System.out.println("名前を入力してください");
17
+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18
-
19
- String strx = br.readLine();
20
18
 
21
19
 
22
20
 
23
- System.out.println("科目を入力してください");
21
+ System.out.println("名前を入力してください");
24
22
 
25
- String stry = br.readLine();
23
+ String strx = br.readLine();
26
24
 
27
25
 
28
26
 
29
- int nameIndex = -1;
27
+ System.out.println("科目を入力してください");
30
28
 
31
- for (int i = 0; i < name.length; i++) {
29
+ String stry = br.readLine();
32
30
 
33
- if (strx.equals(name[i])) {
34
31
 
35
- nameIndex = i;
36
32
 
37
- break;
33
+ int nameIndex = index(name, strx);
38
34
 
39
- }
35
+ int testIndex = index(test, stry);
36
+
37
+
38
+
39
+ System.out.println("名前:" + strx);
40
+
41
+ System.out.println(stry + ":" + point[nameIndex][testIndex]);
40
42
 
41
43
  }
42
44
 
43
45
 
44
46
 
45
- if (nameIndex == -1) {
47
+ private static int index(String[] array, String value) {
46
48
 
49
+ int index = -1;
50
+
51
+ for (int i = 0; i < array.length; i++) {
52
+
53
+ if (value.equals(array[i])) {
54
+
55
+ index = i;
56
+
57
+ break;
58
+
59
+ }
60
+
61
+ }
62
+
63
+ if (index == -1) {
64
+
47
- throw new IllegalArgumentException(strx);
65
+ throw new IllegalArgumentException(value);
66
+
67
+ }
68
+
69
+
70
+
71
+ return index;
48
72
 
49
73
  }
50
74
 
51
-
52
-
53
- int testIndex = -1;
54
-
55
- for (int i = 0; i < test.length; i++) {
56
-
57
- if (stry.equals(test[i])) {
58
-
59
- testIndex = i;
60
-
61
- break;
62
-
63
- }
64
-
65
- }
66
-
67
-
68
-
69
- if (testIndex == -1) {
70
-
71
- throw new IllegalArgumentException(stry);
72
-
73
- }
74
-
75
-
76
-
77
- System.out.println("名前:" + strx);
78
-
79
- System.out.println(stry + ":" + point[nameIndex][testIndex]);
80
-
81
75
  ```