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); } } }
|