質問編集履歴
2
全容を公開いたしました。まわりくどくてすいません。
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,26 +11,143 @@
|
|
11
11
|
### 該当のソースコード
|
12
12
|
```
|
13
13
|
|
14
|
+
package aaa;
|
15
|
+
import java.awt.BorderLayout;
|
16
|
+
import java.awt.event.ActionEvent;
|
17
|
+
import java.awt.event.ActionListener;
|
18
|
+
|
19
|
+
import javax.swing.JButton;
|
20
|
+
import javax.swing.JComboBox;
|
21
|
+
import javax.swing.JFrame;
|
22
|
+
import javax.swing.JLabel;
|
23
|
+
import javax.swing.JPanel;
|
24
|
+
|
25
|
+
public class GameScreen extends JFrame implements ActionListener {
|
26
|
+
|
27
|
+
|
28
|
+
int answer[]= new int[3];
|
29
|
+
|
30
|
+
int count = 0, hit =0 ,blow =0;
|
31
|
+
|
32
|
+
JComboBox<Integer> btn1 = new JComboBox<Integer>();
|
33
|
+
JComboBox<Integer> btn2 = new JComboBox<Integer>();
|
34
|
+
JComboBox<Integer> btn3 = new JComboBox<Integer>();
|
35
|
+
|
36
|
+
public void init() {
|
37
|
+
|
38
|
+
this.setTitle("game");
|
39
|
+
|
40
|
+
//フレームの設定(タイトル・サイズ・位置
|
41
|
+
this.setTitle("game");
|
42
|
+
this.setBounds(100, 100, 400, 300);
|
43
|
+
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
44
|
+
|
45
|
+
|
46
|
+
JPanel ps = new JPanel();
|
47
|
+
JPanel pn = new JPanel();
|
48
|
+
|
49
|
+
|
50
|
+
JLabel label1 = new JLabel("?");
|
51
|
+
JLabel label2 =new JLabel("?");
|
52
|
+
JLabel label3 =new JLabel("?");
|
53
|
+
JLabel label4 = new JLabel("");
|
54
|
+
JLabel label5 = new JLabel("");
|
55
|
+
label4.setText(String.valueOf(hit));
|
56
|
+
label5.setText(String.valueOf(blow));
|
57
|
+
|
58
|
+
|
59
|
+
pn.add(label1);
|
60
|
+
pn.add(label2);
|
61
|
+
pn.add(label3);
|
62
|
+
this.getContentPane().add(pn,BorderLayout.NORTH);
|
63
|
+
ps.add(label4);
|
64
|
+
ps.add(label5);
|
65
|
+
this.getContentPane().add(ps,BorderLayout.SOUTH);
|
66
|
+
|
67
|
+
|
68
|
+
for(int i=1; i<=6; i++) {
|
69
|
+
btn1.addItem(i);
|
70
|
+
btn2.addItem(i);
|
71
|
+
btn3.addItem(i);
|
72
|
+
}
|
73
|
+
|
74
|
+
JButton btn4 = new JButton("チェック");
|
75
|
+
btn4.addActionListener(this);
|
76
|
+
|
77
|
+
ps.add(btn1);
|
78
|
+
ps.add(btn2);
|
79
|
+
ps.add(btn3);
|
80
|
+
ps.add(btn4);
|
81
|
+
|
82
|
+
this.getContentPane().add(ps,BorderLayout.SOUTH);
|
83
|
+
this.setVisible(true);
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
for (int i = 0; i < answer.length; i++) {
|
88
|
+
boolean sw = false;
|
89
|
+
answer[i] = (int) (Math.random() * 6 + 1);
|
90
|
+
do { sw = false;
|
91
|
+
for (int j = i - 1; j >= 0; j--) {
|
92
|
+
if (answer[i] == answer[j]) {
|
93
|
+
sw= true;
|
94
|
+
answer[i] = (int) (Math.random() * 6 + 1);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
} while (sw == true);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
@Override
|
102
|
+
public void actionPerformed(ActionEvent e) {
|
103
|
+
// TODO 自動生成されたメソッド・スタブ
|
104
|
+
|
105
|
+
|
106
|
+
JButton btn = (JButton)e.getSource();
|
107
|
+
String txt = btn.getText();
|
108
|
+
if (txt.equals("チェック")) {
|
109
|
+
//入力された値を取り出す
|
110
|
+
Integer data1 = (Integer)btn1.getSelectedItem();
|
111
|
+
Integer data2 = (Integer)btn2.getSelectedItem();
|
112
|
+
Integer data3 = (Integer)btn3.getSelectedItem();
|
113
|
+
//答えの数字3つと、入力された数字3つを答え合わせする。
|
114
|
+
|
115
|
+
//配列設定
|
116
|
+
int datas [] = new int[3];
|
117
|
+
datas[0] = data1;
|
118
|
+
datas[1] = data2;
|
119
|
+
datas[2] = data3;
|
120
|
+
|
14
|
-
hit = 0
|
121
|
+
hit = 0;
|
15
|
-
blow = 0;
|
122
|
+
blow = 0;
|
16
|
-
count = 0;
|
123
|
+
count = 0;
|
17
|
-
|
124
|
+
|
18
125
|
for (int i = 0; i < answer.length; i++) {
|
19
|
-
|
126
|
+
for (int j = 0; j < answer.length; j++) {
|
20
|
-
|
127
|
+
if (i == j && datas[i] == answer[j]) {
|
21
|
-
|
128
|
+
hit++;
|
22
|
-
|
129
|
+
}
|
23
|
-
|
130
|
+
else if (datas[i] == answer[j]) {
|
24
|
-
|
131
|
+
blow++;
|
132
|
+
|
25
|
-
|
133
|
+
}
|
26
|
-
|
134
|
+
}
|
27
|
-
|
135
|
+
}
|
28
136
|
|
137
|
+
}
|
138
|
+
|
29
|
-
|
139
|
+
System.out.println("ヒット" + hit + " ブロー" + blow+" 回数"+count);
|
30
140
|
|
141
|
+
|
142
|
+
if (hit == 3) {
|
31
|
-
|
143
|
+
creen cls = new ClearScreen();
|
144
|
+
cls.init();//初期化。
|
145
|
+
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
}
|
32
150
|
```
|
33
|
-
|
34
151
|
### 試したこと
|
35
152
|
|
36
153
|
参考書などを読んで試してみましたが思うような結果になりません。
|
1
質問内容が拙く伝わりにくかったため、書き換えました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
```
|
3
|
-
|
3
|
+
現在コードブレイカーというゲームを作成しております。
|
4
|
+
簡単に言いますと隠された3つの数字を当てるゲームです。
|
5
|
+
その中の処理でfor文を使い、入力された数字とプログラムで作られたランダムの数字を
|
6
|
+
当てていくところがあるのですが
|
4
|
-
|
7
|
+
何度目の挑戦か(入力した回数)を表示させたいです。
|
5
|
-
|
8
|
+
その中で挑戦回数を指定できるようにも指定したいと考えています。
|
6
9
|
```
|
7
10
|
|
8
11
|
### 該当のソースコード
|
@@ -11,7 +14,6 @@
|
|
11
14
|
hit = 0;
|
12
15
|
blow = 0;
|
13
16
|
count = 0;
|
14
|
-
|
15
17
|
//ここから
|
16
18
|
for (int i = 0; i < answer.length; i++) {
|
17
19
|
for (int j = 0; j < answer.length; j++) {
|