写这个程序也为了纪念王江民先生,话说当年他也是写过这个类似程序。

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
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class game extends Applet implements ActionListener{
//定义变量
int num1, num2, num3, jieguo, jilu, error = 0, right = 0, key = 0;
char op;
float percent = 0;
String res, lev;
Image labmag;
AudioClip audio1, audio2;
Label prompt1,prompt2;
TextField qus, ans, info;
Button next, enter, record;
public void init()
{
//初始化图片
labmag = getImage (getCodeBase(),"1.jpg");
//初始化音乐
audio1 = getAudioClip(getDocumentBase(),"Error.au");
audio2 = getAudioClip(getDocumentBase(),"Right.au");
qus = new TextField(15);
//初始化按钮,文本框
ans = new TextField(10);
info = new TextField(30);
enter = new Button("确定");
next = new Button("新题目");
record = new Button("记录");
prompt1 = new Label("问题:");
prompt2 = new Label("结果:");
//建立按钮和文本框
add(prompt1);
add(qus);
add(ans);
add(enter);
add(next);
add(record);
add(prompt2);
add(info);
//三个按钮的监听
next.addActionListener(this);
enter.addActionListener(this);
record.addActionListener(this);
}
public void paint(Graphics g)
{
//绘图
g.drawImage(labmag, 0,0,430,280,this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==next)
{
//随机数生成
num1 = (int)(Math.random()*9);
num2 = (int)(Math.random()*9);
num3 = (int)(Math.random()*3);
switch(num3)
{
case 0 : op = '+'; jilu = num1+num2; break;
case 1 : op = '-'; jilu = num1-num2;break;
case 2 : op = '*'; jilu = num1*num2;break;
}
//考虑到整数除法带来的小数问题,暂时取消了生成除法
qus.setText(""+num1+op+num2+"=");
key = 1;
}
if (e.getSource()==enter)
{
if(ans.getText()!="")
jieguo = Integer.parseInt(ans.getText());
if (jieguo == jilu)
{
res="真棒,答对了!";
audio2.play(); //播放音乐
if (key == 1) //答对只记一次分
{
right++;
key = 0;
}
}
else
{
res = "答错了。别灰心!";
audio1.play(); //播放音乐
error++; //答错扣分
}
info.setText(""+res);
}
if (e.getSource()==record)
{
//级别判定
if (right > 0 && right <= 10)
lev = "懒羊羊";
if (right > 10 && right <= 23)
lev = "暖羊羊";
if (right > 23 && right <= 38)
lev = "沸羊羊";
if (right > 38 && right <= 56)
lev = "美羊羊";
if (right > 56 && right <= 76)
lev = "喜绵绵";
if (right > 76 && right <= 100)
lev = "软绵绵";
if (right > 100)
lev = "天神";
info.setText("答对" + right + "题" + " 答错" + error + "题" + " 等级:" + lev);
}
}
}