teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

変更

2016/04/28 04:15

投稿

root_jp
root_jp

スコア4666

answer CHANGED
@@ -1,41 +1,38 @@
1
1
  ```java
2
- String name[] = { "田中", "加藤", "森田", "北国" };
3
- String test[] = { "国語", "英語", "理科", "社会" };
2
+ public static void main(String[] args) throws Exception {
4
3
 
5
- int point[][] = { { 100, 80, 50, 30 }, { 30, 40, 70, 90 }, { 50, 90, 100, 0 }, { 0, 10, 70, 30 } };
4
+ String name[] = { "田中", "加藤", "森田", "北国" };
5
+ String test[] = { "国語", "英語", "理科", "社会" };
6
6
 
7
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7
+ int point[][] = { { 100, 80, 50, 30 }, { 30, 40, 70, 90 }, { 50, 90, 100, 0 }, { 0, 10, 70, 30 } };
8
8
 
9
- System.out.println("名前を入力してください");
9
+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10
- String strx = br.readLine();
11
10
 
12
- System.out.println("科目を入力してください");
11
+ System.out.println("名前を入力してください");
13
- String stry = br.readLine();
12
+ String strx = br.readLine();
14
13
 
15
- int nameIndex = -1;
16
- for (int i = 0; i < name.length; i++) {
14
+ System.out.println("科目を入力してください");
17
- if (strx.equals(name[i])) {
15
+ String stry = br.readLine();
18
- nameIndex = i;
19
- break;
20
- }
21
- }
22
16
 
23
- if (nameIndex == -1) {
17
+ int nameIndex = index(name, strx);
24
- throw new IllegalArgumentException(strx);
18
+ int testIndex = index(test, stry);
19
+
20
+ System.out.println("名前:" + strx);
21
+ System.out.println(stry + ":" + point[nameIndex][testIndex]);
25
22
  }
26
23
 
24
+ private static int index(String[] array, String value) {
27
- int testIndex = -1;
25
+ int index = -1;
28
- for (int i = 0; i < test.length; i++) {
26
+ for (int i = 0; i < array.length; i++) {
29
- if (stry.equals(test[i])) {
27
+ if (value.equals(array[i])) {
30
- testIndex = i;
28
+ index = i;
31
- break;
29
+ break;
30
+ }
32
31
  }
32
+ if (index == -1) {
33
+ throw new IllegalArgumentException(value);
33
- }
34
+ }
34
35
 
35
- if (testIndex == -1) {
36
+ return index;
36
- throw new IllegalArgumentException(stry);
37
37
  }
38
-
39
- System.out.println("名前:" + strx);
40
- System.out.println(stry + ":" + point[nameIndex][testIndex]);
41
38
  ```