該小程序使用java語言編寫,讓用戶計算10以內的加減乘除法,特別適合小學開始學習加減乘除法的學生,能很好地鍛煉他們的運算能力,并過計分計時的游戲性質引起學習興趣!
運行該程序會彈出一個窗口,按enter鍵開始答題,答完再按enter則進行計分和正確性判斷,并出下一道題!每輪10道題,每題10分,答完自動計時記分,全程操作只需要按enter鍵,無其他按鈕,幼兒園小學生都會玩!
并且在輸入合法性方面,也做了相關校驗和提示!歡迎大家發揮想象力,再此基礎上再豐富和完善!
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
package autoScore; import java.awt.Color; import java.awt.Font; import java.awt.Label; import java.awt.List; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.text.DecimalFormat; import javax.swing.JFrame; public class AutoScore extends JFrame{ Label labRule= new Label(); //規則描述標簽 Label labA= new Label(); //第一個數字標簽 Label labOp= new Label(); //加減乘除標簽 Label labB= new Label(); //第二個數字標簽 Label label5= new Label(); //等于號“=”標簽 Label labWarn= new Label(); //輸入合法性校驗提示標簽 Label labQues= new Label(); //答題列表標簽 Label labResult= new Label(); //判分標簽 TextField txtAnswer= new TextField(); //輸入答案輸入框 int total= 0 ; //記錄答題總數 int right= 0 ; //記錄答題正確數量 int error= 0 ; //記錄答題錯誤數量 int score= 0 ; //記錄答題總分 boolean isOver= false ; //一輪結束后標識 boolean isFirst= true ; //程序第一次運行標識 long startTime; //每一輪運行開始時間 List listDisp= new List(); //答題列表展示框 List listScore= new List(); //分數展示框 public static void main(String[] args){ AutoScore score= new AutoScore(); } public AutoScore(){ init(); setSize( 450 , 630 ); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible( true ); } public void init(){ setLayout( null ); setSize( 450 , 630 ); labRule.setText( "規則:每組十道題,每題10分,按ENTER鍵開始,小數保留2位" ); labRule.setBounds( 36 , 10 , 390 , 72 ); labRule.setFont( new Font( "Dialog" ,Font.PLAIN, 12 )); getContentPane().add(labRule); labA.setText( "x" ); labA.setBounds( 36 , 82 , 36 , 36 ); labA.setFont( new Font( "Dialog" ,Font.PLAIN, 24 )); getContentPane().add(labA); labOp.setText( "+" ); labOp.setFont( new Font( "Dialog" ,Font.PLAIN, 24 )); labOp.setBounds( 72 , 82 , 45 , 36 ); getContentPane().add(labOp); labB.setText( "y" ); labB.setFont( new Font( "Dialog" ,Font.PLAIN, 24 )); labB.setBounds( 118 , 82 , 33 , 36 ); getContentPane().add(labB); label5.setText( "=" ); label5.setFont( new Font( "Dialog" ,Font.PLAIN, 24 )); label5.setBounds( 168 , 82 , 24 , 36 ); getContentPane().add(label5); labWarn.setFont( new Font( "Dialog" ,Font.PLAIN, 12 )); labWarn.setBackground(Color.RED); labWarn.setBounds( 320 , 82 , 80 , 36 ); labWarn.setVisible( false ); getContentPane().add(labWarn); labQues.setText( "答題列表:" ); labQues.setFont( new Font( "Dialog" ,Font.PLAIN, 12 )); labQues.setBounds( 36 , 148 , 100 , 20 ); getContentPane().add(labQues); labResult.setText( "分數統計:" ); labResult.setFont( new Font( "Dialog" ,Font.PLAIN, 12 )); labResult.setBounds( 36 , 420 , 100 , 20 ); labResult.setVisible( false ); getContentPane().add(labResult); txtAnswer.setFont( new Font( "Dialog" ,Font.PLAIN, 24 )); txtAnswer.setBounds( 216 , 82 , 100 , 36 ); getContentPane().add(txtAnswer); listDisp.setFont( new Font( "Dialog" ,Font.PLAIN, 16 )); listDisp.setBounds( 36 , 174 , 282 , 230 ); getContentPane().add(listDisp); listScore.setFont( new Font( "Dialog" ,Font.PLAIN, 16 )); listScore.setBounds( 36 , 450 , 282 , 135 ); MyKey myKey= new MyKey(); // 答案輸入框,鍵盤按鍵監聽類 txtAnswer.addKeyListener(myKey); } class MyKey implements KeyListener{ @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if (e.getSource()==txtAnswer){ if (e.getKeyCode()==KeyEvent.VK_ENTER){ if (isOver || isFirst){ updateQuestion( null ); } else if ( "" .equals(txtAnswer.getText())){ labWarn.setText( "請輸入答案!" ); labWarn.setVisible( true ); } else { labWarn.setVisible( false ); if (!isNumber(txtAnswer.getText())){ labWarn.setText( "請輸入數字!" ); labWarn.setVisible( true ); } else if (total< 9 ){ judge( null ); updateQuestion( null ); } else { judge( null ); labResult.setVisible( true ); scorePerformed( null ); } } } } } @Override public void keyReleased(KeyEvent e) { } } int a= 0 ,b= 0 ; String op= "" ; double result= 0 ; DecimalFormat df= new DecimalFormat( "#.00" ); /** * 出題方法 * @param e */ public void updateQuestion(ActionEvent e){ if (isFirst){ startTime=System.currentTimeMillis(); } if (isOver== true ){ listDisp.clear(); listScore.clear(); labResult.setVisible( false ); listScore.setVisible( false ); } isOver= false ; a=( int )(Math.random()* 9 + 1 ); b=( int )(Math.random()* 9 + 1 ); int c=( int )(Math.random()* 4 ); switch (c) { case 0 : op= "+" ; result=a+b; break ; case 1 : op= "-" ; result=a-b; break ; case 2 : op= "*" ; result=a*b; break ; case 3 : op= "/" ; result=(a* 1.0 )/b; //若兩個數除不盡,則按四舍五入保留2位小數 if (String.valueOf(result).length()> 10 ){ result=Double.parseDouble(df.format((a* 1.0 )/b)); } break ; } labA.setText(String.valueOf(a)); labB.setText(String.valueOf(b)); labOp.setText(op); label5.setText( "=" ); txtAnswer.setText( "" ); isFirst= false ; } /** * 判斷結果 * @param e */ public void judge(ActionEvent e){ try { double value=Double.parseDouble(txtAnswer.getText()); String resultStr=(total+ 1 )+ "、 " +a+op+b+ "=" +value; if (value==result){ resultStr+= " \t正確" ; right++; score+= 10 ; } else { resultStr+= " \t錯誤 正確答案:" +result; error++; } listDisp.add(resultStr); total++; } catch (NumberFormatException ignored){ } } /** * 統計分數 * @param e */ public void scorePerformed(ActionEvent e){ isOver= true ; listScore.clear(); listScore.setVisible( true ); String exitStr= "本次共答題" +total+ "道" ; listScore.add(exitStr); listScore.add( "累計用時:" +(System.currentTimeMillis()-startTime)/ 1000 + "秒" ); listScore.add( "答對:" +right+ "個" ); listScore.add( "答錯:" +error+ "個" ); listScore.add( "得分:" +score+ "分" ); getContentPane().add(listScore); score= 0 ; right= 0 ; error= 0 ; total= 0 ; } /** * 校驗輸入是否位數字 * @param numberStr * @return */ public boolean isNumber(String numberStr){ boolean isNumber= true ; try { Double.parseDouble(numberStr); } catch (NumberFormatException e){ isNumber= false ; } return isNumber; } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/guohan_solft/article/details/73322938