teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

コード全文を記載

2020/09/26 17:09

投稿

mi-s.322
mi-s.322

スコア3

title CHANGED
File without changes
body CHANGED
@@ -11,21 +11,88 @@
11
11
  変数hitCount(部分正解の数)をインクリメントしているつもりなのですが、
12
12
  なぜか0のままで動きません・・・。
13
13
 
14
- 一部抜粋なので分かりにくいかと思いますが、
15
14
  どなたかお力添えいただけませんでしょうか。
16
15
 
17
16
  ### 該当のソースコード
18
17
 
19
18
  ```Java
19
+ package kazuate_15_4;
20
+
21
+ public class Main {
22
+ public static void main(String[] args) {
23
+ System.out.println("数あてゲームをはじめます!");
24
+ QuizMan q = new QuizMan();
25
+
26
+ do {
27
+ q.makesAQuestion();
28
+ q.compares();}
29
+ while(q.judges());
30
+ }
31
+
32
+ }
33
+ ---
34
+ package kazuate_15_4;
35
+
36
+ public class QuizMan {
37
+
38
+ private final int ANSWER_LENGTH = 3;
20
39
  private int inputCount = 0;
21
40
  private int hitCount = 0;
41
+ private String answer;
42
+ private String[] answerArray;
43
+ private String input;
44
+ private String[] inputArray;
45
+
46
+ public QuizMan() {
47
+ this.answer = "";
48
+
49
+ for (int i = 0; i < this.ANSWER_LENGTH; i++) {
50
+ int num = new java.util.Random().nextInt(9) + 1;
51
+ this.answer += num;
52
+ }
53
+
54
+ this.answerArray = this.answer.split("");
55
+
56
+ // // テスト
57
+ // for (int j = 0; j < this.answerArray.length; j++) {
58
+ // System.out.println(this.answerArray[j]);
59
+ // }
60
+ }
61
+
62
+ public void makesAQuestion() {
63
+ System.out.println("各桁ごとに1~9の範囲で、" + this.ANSWER_LENGTH + "桁の数字を入力してください");
64
+ String str = new java.util.Scanner(System.in).nextLine();
65
+ this.checksInput(str);
66
+ }
67
+
68
+ public void checksInput(String str) {
69
+ if (str.contains("0")) {
70
+ System.out.println("0が含まれています。最初からやりなおし\n");
71
+ this.makesAQuestion();
72
+ } else if (str.length() != this.ANSWER_LENGTH) {
73
+ System.out.println("桁数がまちがっています。最初からやりなおし\n");
74
+ this.makesAQuestion();
22
- ---
75
+ } else {
76
+ this.input = str;
77
+ this.inputArray = this.input.split("");
78
+ }
79
+ }
80
+
23
81
  public void compares() {
82
+
83
+ // // テスト
84
+ // for (int k = 0; k < this.answerArray.length; k++) {
85
+ // System.out.println(inputArray[k]);
86
+ // }
87
+
24
- for (int i = 0; i < this.answerArray.length; i++) {
88
+ for (int i = 0; i < answerArray.length; i++) {
25
89
  if (this.answerArray[i] == this.inputArray[i]) {
26
90
  this.hitCount++;
27
91
  }
28
92
  }
93
+
94
+ // // テスト
95
+ // System.out.println(this.hitCount);
29
96
  }
30
97
 
31
98
  public boolean judges() {
@@ -41,4 +108,7 @@
41
108
 
42
109
  return true;
43
110
  }
111
+
112
+ }
113
+ }
44
114
  ```

1

誤字

2020/09/26 17:09

投稿

mi-s.322
mi-s.322

スコア3

title CHANGED
File without changes
body CHANGED
@@ -4,8 +4,9 @@
4
4
 
5
5
  ### 発生している問題・エラーメッセージ
6
6
 
7
- なお、配列変数answerArrayと、inputArrayには、
7
+ 配列変数answerArrayと、inputArrayには、
8
- 一桁ずつ数字が格納されていることを確認済みです。
8
+ 一桁ずつ数字が格納されす。
9
+
9
10
  answerArrayとinputArrayで、一致している桁がある場合に
10
11
  変数hitCount(部分正解の数)をインクリメントしているつもりなのですが、
11
12
  なぜか0のままで動きません・・・。