回答編集履歴

1

1

2017/05/28 08:55

投稿

takasima20
takasima20

スコア7458

test CHANGED
@@ -1,3 +1,69 @@
1
1
  個人的には無限ループ部分を関数にして return で抜ける
2
2
 
3
3
  って方がこのみかな。ただし、場合によりけりですが。
4
+
5
+ --- 追記 ---
6
+
7
+ 解決したようですね。
8
+
9
+ ちなみに、こんなコードはどうでしょうか。
10
+
11
+ ```Java
12
+
13
+ public class Number {
14
+
15
+ public static void main (String[] args) {
16
+
17
+ System.out.println("数当てゲームをします");
18
+
19
+ boolean keep = true;
20
+
21
+ int number[] = {3,4,9};
22
+
23
+ while(keep) {
24
+
25
+ keep = quest(number);
26
+
27
+ }
28
+
29
+ System.out.println("ゲームを終了します");
30
+
31
+ }
32
+
33
+ public static boolean quest(number) {
34
+
35
+ boolean ret = true;
36
+
37
+ System.out.println("一桁の数字を入力してください");
38
+
39
+ for (int n: number) {
40
+
41
+ int input = new java.util.Scanner(System.in).nextInt();
42
+
43
+ //正解した時の処理
44
+
45
+ if (input == n) {
46
+
47
+ System.out.println("あたり");
48
+
49
+ ret = false;
50
+
51
+ break;
52
+
53
+ }
54
+
55
+ else {
56
+
57
+ System.out.println("残念もう一度");
58
+
59
+ }
60
+
61
+ }
62
+
63
+ return ret;
64
+
65
+ }
66
+
67
+ }
68
+
69
+ ```