回答編集履歴
1
コードを少し改良
answer
CHANGED
@@ -6,24 +6,29 @@
|
|
6
6
|
public class aaa {
|
7
7
|
|
8
8
|
static int scanInt(Scanner scan) throws Exception {
|
9
|
+
try {
|
9
|
-
|
10
|
+
return Integer.parseInt(scan.next());
|
11
|
+
}
|
12
|
+
catch(NumberFormatException e) {
|
13
|
+
throw new Exception("error:半角数字で入力してください。");
|
14
|
+
}
|
10
15
|
}
|
11
16
|
|
12
17
|
static boolean scanYesNo(Scanner scan) throws Exception {
|
13
18
|
switch(scan.next()) {
|
14
19
|
case "y": return true;
|
15
20
|
case "n": return false;
|
16
|
-
default: throw new Exception();
|
21
|
+
default: throw new Exception("error:yまたはnを入力してください");
|
17
22
|
}
|
18
23
|
}
|
19
24
|
|
20
|
-
static boolean loopIfException(Callable<Boolean> func
|
25
|
+
static boolean loopIfException(Callable<Boolean> func) {
|
21
26
|
while(true) {
|
22
27
|
try {
|
23
28
|
return func.call();
|
24
29
|
}
|
25
30
|
catch(Exception e) {
|
26
|
-
System.out.println(
|
31
|
+
System.out.println(e.getMessage());
|
27
32
|
}
|
28
33
|
}
|
29
34
|
}
|
@@ -38,10 +43,10 @@
|
|
38
43
|
int answer = (int)Math.pow(a, b);
|
39
44
|
System.out.println(a + "の" + b + "乗は" + answer + "です。");
|
40
45
|
return true;
|
41
|
-
}
|
46
|
+
});
|
42
47
|
System.out.println("計算を続けますか?[y/n]");
|
43
48
|
}
|
44
|
-
while(loopIfException(() -> scanYesNo(scan)
|
49
|
+
while(loopIfException(() -> scanYesNo(scan)));
|
45
50
|
System.out.println("計算を終了しました。");
|
46
51
|
}
|
47
52
|
|