質問編集履歴
3
誤字を修正しました
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -92,5 +92,5 @@
|
|
|
92
92
|
|
|
93
93
|
}
|
|
94
94
|
```
|
|
95
|
-
このコードでは、
|
|
95
|
+
このコードでは、inputColourがnameColourと全く同じでなければ、警告メッセージがプリントされました。
|
|
96
96
|
アドバイスをお願いします。
|
2
コードを加筆、コメントを編集しました。
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Javaで
|
|
1
|
+
Javaでマスターマインドを作成しているプログラミング初心者です。
|
|
2
2
|
|
|
3
3
|
Userからのinputを取得し、作成済みのString[]の全ての要素と比較したいです。
|
|
4
4
|
|
|
@@ -10,16 +10,86 @@
|
|
|
10
10
|
②から④を一定回数繰り返す。
|
|
11
11
|
というプロセスなのですが、コードを書いてみても思う結果が得られません。
|
|
12
12
|
|
|
13
|
-
以下そのコードです。
|
|
13
|
+
以下そのコードです。作成段階なので完成していません。
|
|
14
14
|
```Java
|
|
15
|
+
public static void main(String[] args){
|
|
16
|
+
Scanner scan = new Scanner(System.in);
|
|
17
|
+
|
|
18
|
+
boolean condition = true;
|
|
15
|
-
int
|
|
19
|
+
int numberPeg = 0;
|
|
20
|
+
String [] orderPeg = null;
|
|
21
|
+
int numberColour = 0;
|
|
22
|
+
String[] nameColour=null;
|
|
23
|
+
|
|
24
|
+
System.out.println("Enter the number of pegs.");
|
|
25
|
+
try{
|
|
16
|
-
while(
|
|
26
|
+
while(condition){
|
|
17
|
-
|
|
27
|
+
numberPeg = scan.nextInt();
|
|
18
|
-
|
|
28
|
+
if(numberPeg<9){
|
|
19
|
-
|
|
29
|
+
orderPeg = new String [numberPeg];
|
|
20
|
-
|
|
30
|
+
condition=false;
|
|
31
|
+
}
|
|
32
|
+
else{System.err.println("Enter the number between 3-8.");}
|
|
33
|
+
}
|
|
21
34
|
}
|
|
35
|
+
catch(InputMismatchException ime){System.err.println("Input valid argument.");}
|
|
36
|
+
condition=true;
|
|
37
|
+
|
|
38
|
+
System.out.println("Enter the number of colours.");
|
|
39
|
+
try{
|
|
40
|
+
while(condition){
|
|
22
|
-
|
|
41
|
+
numberColour = scan.nextInt();
|
|
42
|
+
if(numberColour<=numberPeg){
|
|
43
|
+
nameColour = new String [numberColour];
|
|
44
|
+
condition=false;
|
|
45
|
+
}
|
|
46
|
+
else{
|
|
47
|
+
System.err.println("Enter appropriate number.");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch(InputMismatchException ime){System.err.println("Input valid argument.");}
|
|
52
|
+
condition=true;
|
|
53
|
+
|
|
54
|
+
System.out.println("Enter colours.");
|
|
55
|
+
try{
|
|
56
|
+
try{
|
|
57
|
+
int counter=0;
|
|
58
|
+
while(counter<nameColour.length){
|
|
59
|
+
String inputColour = scan.next();
|
|
60
|
+
if(inputColour.equals("blue")||inputColour.equals("green")||inputColour.equals("orange")||inputColour.equals("purple")){
|
|
61
|
+
nameColour[counter]=inputColour;
|
|
62
|
+
counter++;
|
|
63
|
+
}
|
|
64
|
+
else{System.err.println("Colours must be blue, green, orange or purple.");}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch(NullPointerException npe){}
|
|
68
|
+
}
|
|
69
|
+
catch(InputMismatchException ime){System.err.println("Input valid argument.");}
|
|
70
|
+
|
|
71
|
+
//ここが問題の箇所です
|
|
72
|
+
System.out.println("Enter the colour of the order.");
|
|
73
|
+
try{
|
|
74
|
+
try{
|
|
75
|
+
int counter=0;
|
|
76
|
+
while(counter<numberPeg){
|
|
77
|
+
String inputColour = scan.next();
|
|
78
|
+
//ここで比較する際に、nameColourの0番目の要素だけでなく、全ての要素と比較して、inputColourがnameColourに含まれているのか確かめたいです。
|
|
79
|
+
if(inputColour.contains(nameColour[counter])){
|
|
80
|
+
orderPeg[counter]=inputColour;
|
|
81
|
+
counter++;
|
|
82
|
+
}
|
|
83
|
+
else{System.err.println("Must be the same colour you chose before.");}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch(NullPointerException npe){}
|
|
87
|
+
}
|
|
88
|
+
catch(InputMismatchException ime){System.err.println("Input valid argument.");}
|
|
89
|
+
|
|
90
|
+
System.out.println("GAME START");
|
|
91
|
+
System.out.println("The number of pegs are "+numberPeg);
|
|
92
|
+
|
|
23
93
|
}
|
|
24
94
|
```
|
|
25
95
|
このコードでは、inputがexistingStringと全く同じでなければ、警告メッセージがプリントされました。
|
1
初心者マークを付加しました
title
CHANGED
|
File without changes
|
body
CHANGED
|
File without changes
|