代码如下:
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 public class Size {
5 public static void main(String[] args) {
6 new Frame();
7
8 }
9 }
10 import javax.swing.*;
11
12 import java.awt.event.*;
13 import java.math.BigDecimal;
14 import java.text.DecimalFormat;
15 import java.text.NumberFormat;
16
17 class Frame extends JFrame implements ActionListener
18 {
19 int a, b, c, count, num;//a,b分别是第一个操作数,第二个操作数,c是存放用户输入的答案,count是题目总数,num是用于存放代表运算符的数
20 int t=0,f=0,sum=0,s=1;//s表示阶乘结果
21 float d,d1;//d是用于存放除法结果,d1是用于处理除法结果中保留小数点后两位
22 int counter=0;//统计答题个数
23 Timer time;
24 int t1 = 0;
25 JTextField T;//定义一个文本
26 JLabel L1 = new JLabel("请输入题目个数(1-5):");
27 JTextField T1 = new JTextField(5); //放题目个数
28 JButton B3 = new JButton("开始");
29 JTextField T2 = new JTextField(10); //设置运算式
30 JTextField T3 = new JTextField(10); //获取输入答案
31 JTextField T4 = new JTextField(10);//存放答案提示
32 JButton B1 = new JButton("确定");
33 JButton B2 = new JButton("下一题");
34
35 Frame() {
36 T=new JTextField(10);//计时
37 T.setEditable(false);
38 time = new Timer(1000, this);
39 time.start();
40 T.setText(setTime(t1));
41 this.setTitle("四则运算");
42 this.setSize(280, 200);
43 this.setVisible(true);
44 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
45 JPanel p2 = new JPanel();
46 JPanel p1 = new JPanel();
47 p2.add(L1);
48 p2.add(T1);
49 p2.add(B3);
50 p2.add(T2);
51 p2.add(T3);
52 p2.add(T4);
53 p2.add(T);
54 p1.add(B1);
55 p1.add(B2);
56 add(p2, "Center");
57 add(p1, "South");
58
59 B3.addActionListener(new ActionListener() { //按钮B3表示开始
60 public void actionPerformed(ActionEvent e) {
61 boolean flag=false;
62 Object bj = e.getSource(); // 获得事件源
63 if (bj == B3) {
64 try{ //如果输入字符,抛出异常,提示错误
65 count = Integer.parseInt(T1.getText()); //将文本中获取的字符串转换为整型
66 }
67 catch(Exception f)
68 {
69 flag=true;
70 }
71 if(flag)
72 {
73 JOptionPane.showMessageDialog(null,"输入有误,请输入数字!");
74 }
75 if(count>5)
76 {
77 JOptionPane.showMessageDialog(null, "题目个数不能超过五个!请重新输入。");
78 }
79 if(count<=5)
80 {
81 num = (int) (Math.random() * 5);
82 a = (int) (Math.random() * 200) - 100;
83 b = (int) (Math.random() * 200) - 100;
84 if (num == 0)//num为0时,是加法运算
85 {
86 if (a <0 && b <0) //判断a,b的正负并给负数加括号
87 {
88 T2.setText("(" + a + ")" + "+(" + b + ")=");
89 }
90 else if (a <0 && b > 0)
91 {
92 T2.setText("(" + a + ")" + "+" + b + "=");
93 }
94 else if (a > 0 && b <0)
95 {
96 T2.setText(a + "+(" + b + ")=");
97 }
98 else
99 {
100 T2.setText(a + "+" + b + "=");
101 }
102 c = a + b;
103 //处理按钮监听事件
104
105 } else if (num == 1) //num为1表示减法运算
106 {
107 if (a <0 && b <0)
108 {
109 T2.setText("(" + a + ")" + "-(" + b + ")=");
110 }
111 else if (a <0 && b > 0)
112 {
113 T2.setText("(" + a + ")" + "-" + b + "=");
114 }
115 else if (a > 0 && b <0)
116 {
117 T2.setText(a + "-(" + b + ")=");
118 }
119 else
120 {
121 T2.setText(a + "-" + b + "=");
122 }
123 c = a - b;
124
125 } else if (num == 2) { //num为2表示乘法运算
126 if (a <0 && b <0) {
127 T2.setText("(" + a + ")" + "*(" + b + ")=");
128
129 } else if (a <0 && b > 0) {
130 T2.setText("(" + a + ")" + "*" + b + "=");
131
132 } else if (a > 0 && b <0) {
133 T2.setText(a + "*(" + b + ")=");
134
135 } else {
136 T2.setText(a + "*" + b + "=");
137
138 }
139 c = a * b;
140 }
141 else if (num == 3&&b!=0) { //num为3表示除法运算,并且要考虑除数不能为0的情况,因此b!=0
142 if (a <0 && b<0) {
143 T2.setText("(" + a + ")" + "/(" + b + ")=");
144
145 } else if (a <0 && b > 0) {
146 T2.setText("(" + a + ")" + "/" + b + "=");
147
148 } else if (a > 0 && b<0) {
149 T2.setText(a + "/(" + b + ")=");
150
151 } else {
152 T2.setText(a + "/" + b + "=");
153
154 }
155 d= (float)a/(float)b; //将整型a,b强制转换为浮点型,保证结果正确
156 BigDecimal bg = new BigDecimal(d); //将除法结果保留小数点后两位
157 d1 =(float)bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
158 }
159 else
160 {
161 a=(int)(Math.random()*10);
162 T2.setText(a+"!=");
163 int i;
164 if(a==0||a==1)
165 {
166 s=1;
167 }
168 else
169 {
170
171 for(i=1;i<=a;i++)//计算阶乘
172 {
173 s=s*i;
174 }
175 }
176 }
177
178 }
179 }
180 }
181 });
182 B2.addActionListener(new ActionListener() { //B2表示下一题
183 public void actionPerformed(ActionEvent e) {
184 Object bj = e.getSource(); // 获得事件源
185 if (bj == B2) {
186 int a, b, num, i = 1;
187 if (i < count) {
188 T3.setText(null);//当按钮按下一题时就将T3与T4的内容更新
189 T4.setText(null);
190 num = (int) (Math.random() * 5);
191 a = (int) (Math.random() * 200) - 100;
192 b = (int) (Math.random() * 200) - 100;
193 i++;
194 if (num == 0) {
195 if (a <0 && b <0) {
196 T2.setText("(" + a + ")" + "+(" + b + ")=");
197 } else if (a <0 && b > 0) {
198 T2.setText("(" + a + ")" + "+" + b + "=");
199 } else if (a > 0 && b <0) {
200 T2.setText(a + "+(" + b + ")=");
201 } else {
202 T2.setText(a + "+" + b + "=");
203 }
204 c = a + b;
205 } else if (num == 1) {
206 if (a <0 && b <0) {
207 T2.setText("(" + a + ")" + "-(" + b + ")=");
208
209 } else if (a <0 && b > 0) {
210 T2.setText("(" + a + ")" + "-" + b + "=");
211 } else if (a > 0 && b <0) {
212 T2.setText(a + "-(" + b + ")=");
213 } else {
214 T2.setText(a + "-" + b + "=");
215 }
216 c = a - b;
217
218 } else if (num == 2) {
219 if (a <0 && b <0) {
220 T2.setText("(" + a + ")" + "*(" + b + ")=");
221
222 } else if (a <0 && b > 0) {
223 T2.setText("(" + a + ")" + "*" + b + "=");
224
225 } else if (a > 0 && b <0) {
226 T2.setText(a + "*(" + b + ")=");
227
228 } else {
229 T2.setText(a + "*" + b + "=");
230
231 }
232 c = a * b;
233
234
235 }
236 else if (num == 3&&b!=0) {
237 if (a <0 && b <0) {
238 T2.setText("(" + a + ")" + "/(" + b + ")=");
239
240 } else if (a <0 && b > 0) {
241 T2.setText("(" + a + ")" + "/" + b + "=");
242 } else if (a > 0 && b <0) {
243 T2.setText(a + "/(" + b + ")=");
244 } else {
245 T2.setText(a + "/" + b + "=");
246 }
247 d= (float)a/(float)b;
248 BigDecimal bg = new BigDecimal(d);
249 d1 =(float)bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
250
251
252 }
253 else
254 {
255 a=(int)(Math.random()*10);
256 T2.setText(a+"!=");
257 if(a==0||a==1)
258 {
259 s=1;
260 }
261 else
262 {
263
264 for(i=1;i<=a;i++)//计算阶乘
265 {
266 s=s*i;
267 }
268 }
269 }
270
271 counter++;
272 if(counter>=count)//判断题目是否答完,答完了就弹出一个提示题目答完并给出答题时间的对话框
273 {
274 JOptionPane.showMessageDialog(null, "题目已答完,答对了"+t+"道题,答错了"+f+"道题\n\n总分是"+sum+"\n\n总用时为:"+t1+"秒");
275 }
276 }
277 }
278 }
279 });
280 B1.addActionListener(new ActionListener() { //按钮B1表示确定
281 public void actionPerformed(ActionEvent e) {
282 Object bj = e.getSource(); // 获得事件源
283 if (bj == B1) {
284 if (d1== Float.parseFloat(T3.getText()) || c == Float.parseFloat(T3.getText()) || s ==Float.parseFloat(T3.getText())) {
285 T4.setText("恭喜你,答对了");
286 t=t+1;
287 sum=sum+20;//每答对一题加20分
288 } else {
289 T4.setText("抱歉,你答错了");
290 f=f+1;
291 }
292 }
293 }
294 });
295
296 }
297 public void actionPerformed(ActionEvent e) {
298 t1= t1 + 1; //累加时间
299 T.setText(setTime(t1));
300
301 }
302 public String setTime(int t1) {//累加时间
303 int hour, minute, second, a;
304 String mytimer, strsec, strmin, strhour;
305 t1=t1 % (60 * 60 * 24);
306 secOnd= t1 % 60;
307 a = t1/ 60;
308 minute = a % 60;
309
310 hour = a / 60;
311 if (second <= 9) {
312 strsec = "0" + String.valueOf(second);
313 } else {
314 strsec = String.valueOf(second);
315 }
316 if (minute <= 9) {
317 strmin = "0" + String.valueOf(minute);
318 } else {
319 strmin = String.valueOf(minute);
320 }
321 if (hour <= 9) {
322 strhour = "0" + String.valueOf(hour);
323 } else {
324 strhour = String.valueOf(hour);
325 }
326 mytimer = "计时: " + strhour + ":" + strmin + ":" + strsec;
327 return mytimer;
328 }
329
330
331 }
运行结果:
这是写代码时的照片