質問編集履歴

4

解決済み

2018/01/04 12:23

投稿

samisa
samisa

スコア7

test CHANGED
File without changes
test CHANGED
@@ -13,227 +13,3 @@
13
13
  わたしが作ったのは、ジャンケンのどれかのボタンを押すと
14
14
 
15
15
  コンピューターがランダムに手を出し、勝敗を示すようになっています
16
-
17
-
18
-
19
- ```import java.awt.*;
20
-
21
- import java.awt.event.*;
22
-
23
- import javax.swing.*;
24
-
25
- import java.util.Random;
26
-
27
-
28
-
29
- public class janken extends JFrame implements ActionListener{
30
-
31
-
32
-
33
- ImageIcon icon1,icon2,icon3;
34
-
35
- JLabel npc_image;
36
-
37
- JLabel result;
38
-
39
-
40
-
41
- private int jankenCounter = 0;
42
-
43
- private int numOfPlayerWon = 0;
44
-
45
-
46
-
47
- int sw = 0;
48
-
49
-
50
-
51
- String my,npc;
52
-
53
-
54
-
55
- public janken(){
56
-
57
-
58
-
59
- setSize(800,800);
60
-
61
-
62
-
63
- setDefaultCloseOperation(EXIT_ON_CLOSE);
64
-
65
-
66
-
67
- icon1 = new ImageIcon("gu.png");
68
-
69
- icon2 = new ImageIcon("tyoki.png");
70
-
71
- icon3 = new ImageIcon("pa.png");
72
-
73
-
74
-
75
- setLayout(new FlowLayout());
76
-
77
-
78
-
79
- JButton b1 = new JButton("ぐー",icon1);
80
-
81
- JButton b2 = new JButton("ちょき",icon2);
82
-
83
- JButton b3 = new JButton("ぱー",icon3);
84
-
85
-
86
-
87
- b1.addActionListener(this);
88
-
89
- b2.addActionListener(this);
90
-
91
- b3.addActionListener(this);
92
-
93
-
94
-
95
- add(b1);
96
-
97
- add(b2);
98
-
99
- add(b3);
100
-
101
-
102
-
103
- npc_image = new JLabel();
104
-
105
- add(npc_image);
106
-
107
-
108
-
109
- result = new JLabel("結果");
110
-
111
- add(result);
112
-
113
-
114
-
115
- }
116
-
117
-
118
-
119
- public void actionPerformed(ActionEvent ae){
120
-
121
-
122
-
123
- my = ae.getActionCommand();
124
-
125
-
126
-
127
- npc = getNpcCommand();
128
-
129
-
130
-
131
- switch (npc) {
132
-
133
- case "ぐー": npc_image.setIcon(icon1); break;
134
-
135
- case "ちょき":npc_image.setIcon(icon2); break;
136
-
137
- case "ぱー": npc_image.setIcon(icon3); break;
138
-
139
- }
140
-
141
-
142
-
143
- repaint();
144
-
145
-
146
-
147
- public void paint(Graphics g) {
148
-
149
- super.paint(g);
150
-
151
-
152
-
153
- if(jankenCounter == 5) {
154
-
155
- if(numOfPlayerWon > 2) {
156
-
157
- g.drawString("あなたの勝ち",750,600);
158
-
159
- }
160
-
161
- else {
162
-
163
- g.drawString("あなたの負け",750,600);
164
-
165
- }
166
-
167
- jankenCounter = 0;
168
-
169
- numOfPlayerWon = 0;
170
-
171
- return;
172
-
173
- }
174
-
175
-
176
-
177
- if((my == "ぐー" && npc == "ぐー")||(my == "ちょき" && npc == "ちょき")||(my == "ぱー" && npc == "ぱー")) {
178
-
179
- }
180
-
181
- else if((my == "ぐー" && npc == "ちょき")||(my == "ちょき" && npc == "ぱー")||(my == "ぱー" && npc == "ぐー")) {
182
-
183
- jankenCounter++;
184
-
185
- numOfPlayerWon++;
186
-
187
- }
188
-
189
- else {
190
-
191
- jankenCounter++;
192
-
193
- }
194
-
195
- }
196
-
197
-
198
-
199
- private static String getNpcCommand() {
200
-
201
- String command;
202
-
203
- switch((int)(Math.random()*3)){
204
-
205
- case 0: command = "ぐー"; break;
206
-
207
- case 1: command = "ちょき";break;
208
-
209
- default:command = "ぱー"; break;
210
-
211
- }
212
-
213
- return command;
214
-
215
- }
216
-
217
-
218
-
219
- public static void main(String[] args) {
220
-
221
-
222
-
223
-
224
-
225
- JFrame w = new janken();
226
-
227
- w.setVisible(true);
228
-
229
- }
230
-
231
- }
232
-
233
-
234
-
235
-
236
-
237
- コード
238
-
239
- ```

3

プログラムの修正

2018/01/04 12:23

投稿

samisa
samisa

スコア7

test CHANGED
File without changes
test CHANGED
@@ -144,14 +144,24 @@
144
144
 
145
145
 
146
146
 
147
+ public void paint(Graphics g) {
148
+
149
+ super.paint(g);
150
+
151
+
152
+
147
153
  if(jankenCounter == 5) {
148
154
 
149
155
  if(numOfPlayerWon > 2) {
150
156
 
157
+ g.drawString("あなたの勝ち",750,600);
158
+
151
159
  }
152
160
 
153
161
  else {
154
162
 
163
+ g.drawString("あなたの負け",750,600);
164
+
155
165
  }
156
166
 
157
167
  jankenCounter = 0;
@@ -164,11 +174,11 @@
164
174
 
165
175
 
166
176
 
167
- if(あいこ) {
177
+ if((my == "ぐー" && npc == "ぐー")||(my == "ちょき" && npc == "ちょき")||(my == "ぱー" && npc == "ぱー")) {
168
-
178
+
169
- }
179
+ }
170
-
180
+
171
- else if(ち) {
181
+ else if((my == "ぐー" && npc == "ょき")||(my == "ちょき" && npc == "ぱー")||(my == "ぱー" && npc == "ぐー")) {
172
182
 
173
183
  jankenCounter++;
174
184
 
@@ -182,75 +192,47 @@
182
192
 
183
193
  }
184
194
 
195
+ }
196
+
197
+
198
+
199
+ private static String getNpcCommand() {
200
+
201
+ String command;
202
+
203
+ switch((int)(Math.random()*3)){
204
+
205
+ case 0: command = "ぐー"; break;
206
+
207
+ case 1: command = "ちょき";break;
208
+
209
+ default:command = "ぱー"; break;
210
+
211
+ }
212
+
213
+ return command;
214
+
185
215
  }
186
216
 
217
+
218
+
219
+ public static void main(String[] args) {
220
+
221
+
222
+
223
+
224
+
225
+ JFrame w = new janken();
226
+
227
+ w.setVisible(true);
228
+
187
- }
229
+ }
188
-
189
-
190
-
191
- public void paint(Graphics g) {
192
-
193
- super.paint(g);
194
-
195
-
196
-
197
- if((my == "ぐー" && npc == "ぐー")||(my == "ちょき" && npc == "ちょき")||(my == "ぱー" && npc == "ぱー")){
198
-
199
- g.drawString("あいこ",700,500);
200
-
201
- }
202
-
203
- else if((my == "ぐー" && npc == "ちょき")||(my == "ちょき" && npc == "ぱー")||(my == "ぱー" && npc == "ぐー")) {
204
-
205
- g.drawString("勝ち",700,500);
206
-
207
- }
208
-
209
- else{
210
-
211
- g.drawString("負け",700,500);
212
-
213
- }
214
-
215
-
216
230
 
217
231
  }
218
232
 
219
233
 
220
234
 
221
- private static String getNpcCommand() {
235
+
222
-
223
- String command;
224
-
225
- switch((int)(Math.random()*3)){
226
-
227
- case 0: command = "ぐー"; break;
228
-
229
- case 1: command = "ちょき";break;
230
-
231
- default:command = "ぱー"; break;
232
-
233
- }
234
-
235
- return command;
236
-
237
- }
238
-
239
-
240
-
241
- public static void main(String[] args) {
242
-
243
-
244
-
245
-
246
-
247
- JFrame w = new janken();
248
-
249
- w.setVisible(true);
250
-
251
- }
252
-
253
- }
254
236
 
255
237
  コード
256
238
 

2

コードの修正

2018/01/04 04:05

投稿

samisa
samisa

スコア7

test CHANGED
File without changes
test CHANGED
@@ -38,6 +38,12 @@
38
38
 
39
39
 
40
40
 
41
+ private int jankenCounter = 0;
42
+
43
+ private int numOfPlayerWon = 0;
44
+
45
+
46
+
41
47
  int sw = 0;
42
48
 
43
49
 
@@ -136,6 +142,48 @@
136
142
 
137
143
  repaint();
138
144
 
145
+
146
+
147
+ if(jankenCounter == 5) {
148
+
149
+ if(numOfPlayerWon > 2) {
150
+
151
+ }
152
+
153
+ else {
154
+
155
+ }
156
+
157
+ jankenCounter = 0;
158
+
159
+ numOfPlayerWon = 0;
160
+
161
+ return;
162
+
163
+ }
164
+
165
+
166
+
167
+ if(あいこ) {
168
+
169
+ }
170
+
171
+ else if(かち) {
172
+
173
+ jankenCounter++;
174
+
175
+ numOfPlayerWon++;
176
+
177
+ }
178
+
179
+ else {
180
+
181
+ jankenCounter++;
182
+
183
+ }
184
+
185
+ }
186
+
139
187
  }
140
188
 
141
189
 
@@ -168,6 +216,8 @@
168
216
 
169
217
  }
170
218
 
219
+
220
+
171
221
  private static String getNpcCommand() {
172
222
 
173
223
  String command;
@@ -192,9 +242,7 @@
192
242
 
193
243
 
194
244
 
195
-
196
-
197
-
245
+
198
246
 
199
247
  JFrame w = new janken();
200
248
 

1

タグの誤表示

2018/01/04 01:07

投稿

samisa
samisa

スコア7

test CHANGED
File without changes
test CHANGED
File without changes