1 package runok; 2 import java.util.*; 3 import java.awt.*; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 import javax.swing.*; 8 public class vv extends JFrame implements ActionListener{ 9 10 /** 11 * 作者:範銘祥 12 * 功能:一個簡單的小學生四則運算 自動 生成系統 13 */ 14 JPanel jp1,jp2,jp3; 15 JLabel jlb1,jlb2; 16 JButton jb1; 17 JTextField jtf1,jtf2; 18 JTextArea ee; 19 public double w1; 20 public double m1; 21 String fh; 22 public static void main(String[] args) 23 { 24 vv start=new vv(); 25 } 26 public vv() 27 { 28 jp1=new JPanel(); 29 jp2=new JPanel(); 30 jp3=new JPanel(); 31 32 jlb1=new JLabel("題目"); 33 jlb2=new JLabel("你的答案:"); 34 35 jb1=new JButton("判斷答案"); 36 jb1.addActionListener(this); 37 jb1.setActionCommand("aa"); 38 JTextArea txtArea = new JTextArea(1,15); 39 jtf2=new JTextField(20); 40 ee = new JTextArea(1,10); 41 //設定佈局管理 42 this.setLayout(new GridLayout(3,1)); 43 //加入各個元件 44 jp1.add(jlb1); 45 jp1.add(txtArea); 46 47 jp2.add(jlb2); 48 jp2.add(jtf2); 49 jp3.add(jb1); 50 jp3.add(ee); 51 //加入到JFrame 52 this.add(jp1); 53 this.add(jp2); 54 this.add(jp3); 55 //在這裡用yy方法來獲取題目 w 符號 m 56 yy tt=new yy(); 57 58 59 w1=tt.ret1(); 60 m1=tt.ret2(); 61 fh=tt.ret3(); 62 txtArea.append(w1+fh+m1); 63 this.setSize(400, 250); 64 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 65 this.setVisible(true); 66 } 67 public void yes() 68 { ee.append("正確!"); } 69 public void no() 70 {ee.append("錯誤!");} 71 public void cc() 72 { 73 int answer=(int) (Double.parseDouble(jtf2.getText()));; 74 if(fh.equals("+")) 75 { 76 if(answer==w1+m1){ 77 yes(); 78 }else{ 79 no(); 80 } 81 } 82 if(fh.equals("-")) 83 { 84 if(answer==w1-m1){ 85 yes(); 86 }else{ 87 no(); 88 } 89 } 90 if(fh.equals("*")) 91 { 92 if(answer==w1*m1){ 93 yes(); 94 }else{ 95 no(); 96 } 97 } 98 if(fh.equals("/")) 99 { 100 if(answer==w1/m1){ 101 yes(); 102 }else{ 103 no(); 104 } 105 } 106 } 107 @Override 108 public void actionPerformed(ActionEvent e) 109 { 110 // !!!! 111 if(e.getActionCommand()=="aa") 112 { 113 cc(); 114 } 115 } 116 117 } 118 119 120 class yy 121 { 122 private int max=4; 123 private int min=1; 124 private double w2,m2; 125 private String ff; 126 Random random = new Random(); 127 128 //System.out.println(s); 129 public yy() 130 { 131 int s = random.nextInt(max)%(max-min+1) + min; 132 133 double x=random.nextInt(max-2)%(max-2-min+1) + min; 134 136 double y=random.nextInt(max-1)%(max-1-min+1) + min; 137 int y1up=(int) Math.pow(10,x); 138 int y1dn=(int) Math.pow(10,x-1); 139 int y2up=(int) Math.pow(10,y); 140 int y2dn=(int) Math.pow(10,y-1); 141 int x_y1=random.nextInt(y1up)%(y1up-y1dn+1) + y1dn; 142 int x_y2=random.nextInt(y2up)%(y2up-y2dn+1) + y2dn; 143 double w=(double)x_y1; 144 double m=(double)x_y2; 145 this.w2=w; 146 this.m2=m; 147 switch(s) 148 { 149 case 1: 150 { 151 this.ff="+"; 152 System.out.println(w+"+"+m+"=" ); 153 break; 154 } 155 case 2: 156 { 157 this.ff="-"; 158 System.out.println(w+"-"+m+"=" ); 159 break; 160 } 161 case 3: 162 { 163 this.ff="*"; 164 System.out.println(w+"*"+m+"=" ); 165 break; 166 } 167 case 4: 168 { 169 this.ff="/"; 170 System.out.println(w+"/"+m+"=" ); 171 break; 172 } 173 174 } 175 } 176 public double ret1() 177 { 178 return w2; 179 } 180 public double ret2() 181 { 182 return m2; 183 } 184 public String ret3() 185 { 186 return ff; 187 } 188 }