回答編集履歴
2
追記
test
CHANGED
@@ -49,3 +49,43 @@
|
|
49
49
|
}
|
50
50
|
|
51
51
|
```
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
実例
|
56
|
+
|
57
|
+
---
|
58
|
+
|
59
|
+
**ダメな例** (2回目以降の呼び出し時にNoSuchElementExceptionを送出する)
|
60
|
+
|
61
|
+
```Java
|
62
|
+
|
63
|
+
int read() {
|
64
|
+
|
65
|
+
Scanner sc = new Scanner(System.in);
|
66
|
+
|
67
|
+
int ret = sc.nextInt();
|
68
|
+
|
69
|
+
sc.close();
|
70
|
+
|
71
|
+
return ret;
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
**良い例**
|
80
|
+
|
81
|
+
```Java
|
82
|
+
|
83
|
+
int read(Scanner sc) {
|
84
|
+
|
85
|
+
int ret = sc.nextInt();
|
86
|
+
|
87
|
+
return ret;
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
```
|
1
追記
test
CHANGED
@@ -9,3 +9,43 @@
|
|
9
9
|
|
10
10
|
|
11
11
|
その場合、mainメソッド終了前にcloseしてあげて下さい。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
エラー再現
|
16
|
+
|
17
|
+
---
|
18
|
+
|
19
|
+
```Java
|
20
|
+
|
21
|
+
import java.util.Scanner;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
class Main {
|
26
|
+
|
27
|
+
public static void main(String[] args) {
|
28
|
+
|
29
|
+
Scanner sc;
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
sc = new Scanner(System.in);
|
34
|
+
|
35
|
+
System.out.println(sc.next()); // ここではちゃんと読める
|
36
|
+
|
37
|
+
sc.close();
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
sc = new Scanner(System.in);
|
42
|
+
|
43
|
+
System.out.println(sc.next()); // エラー!!
|
44
|
+
|
45
|
+
sc.close();
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
```
|