回答編集履歴

2

typo 修正

2016/10/21 13:45

投稿

mit0223
mit0223

スコア3401

test CHANGED
@@ -100,7 +100,7 @@
100
100
 
101
101
  if (ans < 1 || ans > 3) {
102
102
 
103
- System.out.println("1 から 3 の範囲で入力してください");
103
+ errorStream.println("1 から 3 の範囲で入力してください");
104
104
 
105
105
  } else {
106
106
 

1

数値読み込みのエラーチェックのところが気になったので、修正しました。

2016/10/21 13:45

投稿

mit0223
mit0223

スコア3401

test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  private int answer;
26
26
 
27
-
27
+
28
28
 
29
29
  quiz(String q, String c, int a) {
30
30
 
@@ -36,7 +36,7 @@
36
36
 
37
37
  }
38
38
 
39
-
39
+
40
40
 
41
41
  public static quiz readNewQuiz(BufferedReader quizDataReader) throws IOException {
42
42
 
@@ -68,7 +68,7 @@
68
68
 
69
69
  }
70
70
 
71
-
71
+
72
72
 
73
73
  public void printQuestion(int count, PrintStream ps) {
74
74
 
@@ -80,7 +80,7 @@
80
80
 
81
81
  }
82
82
 
83
-
83
+
84
84
 
85
85
  public boolean checkAnswer(int ans) {
86
86
 
@@ -88,7 +88,37 @@
88
88
 
89
89
  }
90
90
 
91
+
91
92
 
93
+ public static int readNumber(BufferedReader br, PrintStream errorStream) throws IOException {
94
+
95
+ while (true) {
96
+
97
+ try {
98
+
99
+ int ans = Integer.parseInt(br.readLine());
100
+
101
+ if (ans < 1 || ans > 3) {
102
+
103
+ System.out.println("1 から 3 の範囲で入力してください");
104
+
105
+ } else {
106
+
107
+ return ans;
108
+
109
+ }
110
+
111
+ } catch (NumberFormatException e) {
112
+
113
+ errorStream.println("数値を入力してください");
114
+
115
+ }
116
+
117
+ }
118
+
119
+ }
120
+
121
+
92
122
 
93
123
  public static void main (String[]args) throws IOException {
94
124
 
@@ -108,23 +138,9 @@
108
138
 
109
139
  q.printQuestion(count, System.out);
110
140
 
111
- int ans = 0;
141
+ int ans = readNumber(br, System.out);
112
142
 
113
- try {
114
-
115
- ans = Integer.parseInt(br.readLine());
116
-
117
- } catch (NumberFormatException e) {
118
-
119
- }
120
-
121
- if (ans < 1 || ans > 3) {
122
-
123
- System.out.println("1 から 3 の数値を入力してください");
124
-
125
- break;
126
-
127
- } else if (q.checkAnswer(ans)) {
143
+ if (q.checkAnswer(ans)) {
128
144
 
129
145
  System.out.println("正解!第" + ++count +"問に進みます。");
130
146
 
@@ -138,9 +154,7 @@
138
154
 
139
155
  }
140
156
 
141
- }
157
+ }
142
-
143
-
144
158
 
145
159
  }
146
160