标签:
1 import java.awt.Dimension; 2 import java.awt.FlowLayout; 3 import java.awt.GridLayout; 4 import java.awt.Toolkit; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 8 import javax.swing.JButton; 9 import javax.swing.JFrame; 10 import javax.swing.JLabel; 11 import javax.swing.JOptionPane; 12 import javax.swing.JPanel; 13 import javax.swing.JTextField; 14 import javax.swing.Timer; 15 16 public class Jiemian extends JFrame implements ActionListener{ 17 18 /** 19 * @param args 20 */ 21 private Timer tmr; 22 String result; 23 int dadui=0; 24 JLabel L1=new JLabel("设置时间:"); 25 JLabel L2=new JLabel("实际用时:"); 26 JLabel L3=new JLabel("输入你想要的题数(最多1--5):"); 27 JLabel L4=new JLabel("你答对的题目数有:"); 28 JLabel L5=new JLabel("温馨提示:"); 29 JLabel L6=new JLabel("="); 30 JLabel Q1=new JLabel(" "); 31 JLabel Q2=new JLabel(" "); 32 JLabel Q3=new JLabel(" "); 33 JLabel Q4=new JLabel(" "); 34 JTextField T1=new JTextField(10); 35 JTextField T2=new JTextField(10); 36 JTextField T3=new JTextField(3); 37 JTextField T4=new JTextField(3); 38 JTextField T5=new JTextField(3); 39 JTextField T6=new JTextField(3); 40 JTextField T7=new JTextField(3); 41 JTextField T8=new JTextField(20); 42 JTextField T9=new JTextField(3); 43 44 JButton B1=new JButton("确定"); 45 JButton B5=new JButton("确定"); 46 JButton B2=new JButton("出题"); 47 JButton B3=new JButton("确定答案!"); 48 JButton B4=new JButton("下一题!"); 49 50 51 52 53 54 public Jiemian(){ 55 super("CHENGXU"); 56 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 57 this.setPreferredSize(new Dimension(300,600)); 58 this.getContentPane(); 59 this.pack(); 60 FlowLayout layout = new FlowLayout(FlowLayout.CENTER,5,20); 61 this.setLayout(layout); 62 this.add(L1);this.add(T1);this.add(B1);this.add(L2);this.add(T2);this.add(Q1); 63 this.add(L3);this.add(T3);this.add(B5); 64 this.add(B2);this.add(B2); 65 this.add(T4);this.add(T5);this.add(T6);this.add(L6);this.add(T7); 66 this.add(B3);this.add(B4);this.add(Q2); 67 this.add(L5);this.add(T8); 68 this.add(L4);this.add(T9);this.add(Q3); 69 70 B1.addActionListener(this); 71 72 B2.addActionListener(this); 73 74 B3.addActionListener(this); 75 Timer tmr = new Timer(1000, this);//间隔1秒 76 this.tmr = tmr;//添加时间 77 B4.addActionListener(this); 78 79 B5.addActionListener(this); 80 81 82 int width = Toolkit.getDefaultToolkit().getScreenSize().width; //把框在屏幕中间显示 83 int height = Toolkit.getDefaultToolkit().getScreenSize().height; 84 this.setLocation(width / 2 - 300, height / 2 - 200); 85 86 this.setVisible(true); 87 this.setResizable(false); 88 89 90 91 } 92 93 94 95 96 public void actionPerformed(ActionEvent e) 97 { 98 if(e.getSource()==B1)//当获取的资源是按钮1时 99 { 100 try 101 { 102 T2.setText(String.valueOf(T1.getText()));//在显示时间框里显示以获取的时间 103 104 int n1=120,n2=5; 105 if(n1<(Integer.parseInt(T2.getText()))) 106 {//当设置时间超过120秒,弹出对话框 107 JOptionPane.showMessageDialog(null, "时间不能超过120秒!"); 108 this.setVisible(false); 109 } 110 if(n2<(Integer.parseInt(T3.getText()))) 111 {//当选择题目个数超过5个,弹出对话框 112 JOptionPane.showMessageDialog(null, "选择题数不能超过五个!"); 113 this.setVisible(false); 114 } 115 tmr.start();//开始计时 116 } 117 catch(NullPointerException o) 118 { 119 120 } 121 //suijishu();//调用产生随机数题目 122 // startTime= System.currentTimeMillis(); //获取开始时间 123 T2.setText(String.valueOf(T1.getText())); 124 } 125 else{ 126 int m = Integer.parseInt(T2.getText()); 127 m--; 128 T2.setText("" + m); 129 if (m <= 0) 130 { 131 tmr.stop(); 132 JOptionPane.showMessageDialog(null, "超出答题时间!"); 133 this.setVisible(false); 134 } 135 } 136 137 138 if(e.getSource()==B5)//当获取的资源是按钮1时 139 { 140 int num=(int) Double.parseDouble(T3.getText()); 141 System.out.println(num); 142 143 if(5<(Integer.parseInt(T3.getText()))) 144 {//当选择题目个数超过5个,弹出对话框 145 JOptionPane.showMessageDialog(null, "选择题数不能超过五个!"); 146 this.setVisible(false); 147 } 148 149 } 150 151 152 153 154 if(e.getSource()==B2)//当获取的资源是按钮1时 155 { 156 int fuhao1=(int) (Math.random()*11/3+1); 157 switch (fuhao1) 158 { 159 case 1: 160 { 161 int leixing=(int) (Math.random()*8/3+1);//1表示整数,2表示分数,3表示阶乘; 162 switch(leixing) 163 { 164 case 1: 165 { 166 int shu1=(int) (Math.random()*10+1); 167 int shu2=(int) (Math.random()*10+1); 168 T4.setText(String.valueOf(shu1)); 169 T6.setText(String.valueOf(shu2)); 170 T5.setText(String.valueOf("+")); 171 result=String.valueOf(shu1+shu2); 172 if(e.getSource()==B3) 173 { 174 if(result==T7.getText()) 175 { 176 T8.setText("答案正确!"); 177 } 178 else 179 T8.setText("答案错误!正确答案是"+result); 180 dadui++; 181 } 182 183 }break; 184 case 2: 185 { 186 int shu1=(int) (Math.random()*10+1); 187 int shu2=(int) (Math.random()*10+1); 188 int shu3=(int) (Math.random()*10+1); 189 int shu4=(int) (Math.random()*10+1); 190 if(shu1>shu2) 191 { 192 int temp=shu1; 193 shu1=shu2; 194 shu2=temp; 195 } 196 if(shu3>shu4) 197 { 198 int temp=shu3; 199 shu3=shu4; 200 shu4=temp; 201 } 202 203 T4.setText(String.valueOf(shu1+"/"+shu2)); 204 T6.setText(String.valueOf(shu3+"/"+shu4)); 205 T5.setText(String.valueOf("+")); 206 int res1=(shu1*shu4)+(shu2*shu3); 207 int res2=shu2*shu4; 208 int pan=0; 209 if(res1>res2) 210 { 211 pan=res2; 212 } 213 else 214 { 215 pan=res1; 216 } 217 for(int i=pan;i>0;i--) 218 { 219 if(res1%pan==0&&res2%pan==0) 220 { 221 res1=res1/pan; 222 res2=res2/pan; 223 if(res1>res2) 224 { 225 pan=res2; 226 } 227 else 228 { 229 pan=res1; 230 } 231 i=pan; 232 } 233 } 234 235 result=String.valueOf(shu1+"/"+shu2); 236 if(e.getSource()==B3) 237 { 238 if(result==T7.getText()) 239 { 240 T8.setText("答案正确!"); 241 } 242 else 243 T8.setText("答案错误!正确答案是"+result); 244 dadui++; 245 } 246 }break; 247 case 3: 248 { 249 250 int shu1=(int) (Math.random()*9+1); 251 int res1=shu1; 252 T6.setText(String.valueOf("!"+shu1)); 253 for(int i=shu1-1;i>0;i--) 254 { 255 res1=res1*i; 256 } 257 result=String.valueOf(res1); 258 if(e.getSource()==B3) 259 { 260 if(result==T7.getText()) 261 { 262 T8.setText("答案正确!"); 263 } 264 else 265 T8.setText("答案错误!正确答案是"+result); 266 dadui++; 267 } 268 }break; 269 270 } 271 272 }break; 273 case 2: 274 { 275 int leixing=(int) (Math.random()*8/3+1);//1表示整数,2表示分数,3表示阶乘; 276 switch(leixing) 277 { 278 case 1: 279 { 280 int shu1=(int) (Math.random()*10+1); 281 int shu2=(int) (Math.random()*10+1); 282 T4.setText(String.valueOf(shu1)); 283 T6.setText(String.valueOf(shu2)); 284 T5.setText(String.valueOf("-")); 285 result=String.valueOf(shu1-shu2); 286 if(e.getSource()==B3) 287 { 288 if(result==T7.getText()) 289 { 290 T8.setText("答案正确!"); 291 } 292 else 293 T8.setText("答案错误!正确答案是"+result); 294 dadui++; 295 } 296 }break; 297 case 2: 298 { 299 int shu1=(int) (Math.random()*10+1); 300 int shu2=(int) (Math.random()*10+1); 301 int shu3=(int) (Math.random()*10+1); 302 int shu4=(int) (Math.random()*10+1); 303 if(shu1>shu2) 304 { 305 int temp=shu1; 306 shu1=shu2; 307 shu2=temp; 308 } 309 if(shu3>shu4) 310 { 311 int temp=shu3; 312 shu3=shu4; 313 shu4=temp; 314 } 315 316 T4.setText(String.valueOf(shu1+"/"+shu2)); 317 T6.setText(String.valueOf(shu3+"/"+shu4)); 318 T5.setText(String.valueOf("-")); 319 int res1=(shu1*shu4)-(shu2*shu3); 320 int res2=shu2*shu4; 321 int pan=0; 322 if(res1>res2) 323 { 324 pan=res2; 325 } 326 else 327 { 328 pan=res1; 329 } 330 for(int i=pan;i>0;i--) 331 { 332 if(res1%pan==0&&res2%pan==0) 333 { 334 res1=res1/pan; 335 res2=res2/pan; 336 if(res1>res2) 337 { 338 pan=res2; 339 } 340 else 341 { 342 pan=res1; 343 } 344 i=pan; 345 } 346 } 347 348 result=String.valueOf(shu1+"/"+shu2); 349 if(e.getSource()==B3) 350 { 351 if(result==T7.getText()) 352 { 353 T8.setText("答案正确!"); 354 } 355 else 356 T8.setText("答案错误!正确答案是"+result); 357 dadui++; 358 } 359 }break; 360 case 3: 361 { 362 363 int shu1=(int) (Math.random()*9+1); 364 int res1=shu1; 365 T6.setText(String.valueOf("!"+shu1)); 366 for(int i=shu1-1;i>0;i--) 367 { 368 res1=res1*i; 369 } 370 result=String.valueOf(res1); 371 if(e.getSource()==B3) 372 { 373 if(result==T7.getText()) 374 { 375 T8.setText("答案正确!"); 376 } 377 else 378 T8.setText("答案错误!正确答案是"+result); 379 dadui++; 380 } 381 }break; 382 383 } 384 385 }break; 386 case 3: 387 { 388 int leixing=(int) (Math.random()*8/3+1);//1表示整数,2表示分数,3表示阶乘; 389 switch(leixing) 390 { 391 case 1: 392 { 393 int shu1=(int) (Math.random()*10+1); 394 int shu2=(int) (Math.random()*10+1); 395 T4.setText(String.valueOf(shu1)); 396 T6.setText(String.valueOf(shu2)); 397 T5.setText(String.valueOf("*")); 398 result=String.valueOf(shu1*shu2); 399 if(e.getSource()==B3) 400 { 401 if(result==T7.getText()) 402 { 403 T8.setText("答案正确!"); 404 } 405 else 406 T8.setText("答案错误!正确答案是"+result); 407 dadui++; 408 } 409 }break; 410 case 2: 411 { 412 int shu1=(int) (Math.random()*10+1); 413 int shu2=(int) (Math.random()*10+1); 414 int shu3=(int) (Math.random()*10+1); 415 int shu4=(int) (Math.random()*10+1); 416 if(shu1>shu2) 417 { 418 int temp=shu1; 419 shu1=shu2; 420 shu2=temp; 421 } 422 if(shu3>shu4) 423 { 424 int temp=shu3; 425 shu3=shu4; 426 shu4=temp; 427 } 428 429 T4.setText(String.valueOf(shu1+"/"+shu2)); 430 T6.setText(String.valueOf(shu4+"/"+shu3)); 431 T5.setText(String.valueOf("*")); 432 int res1=(shu1)*(shu3); 433 int res2=shu2*shu4; 434 int pan=0; 435 if(res1>res2) 436 { 437 pan=res2; 438 } 439 else 440 { 441 pan=res1; 442 } 443 for(int i=pan;i>0;i--) 444 { 445 if(res1%pan==0&&res2%pan==0) 446 { 447 res1=res1/pan; 448 res2=res2/pan; 449 if(res1>res2) 450 { 451 pan=res2; 452 } 453 else 454 { 455 pan=res1; 456 } 457 i=pan; 458 } 459 } 460 461 result=String.valueOf(shu1+"/"+shu2); 462 if(e.getSource()==B3) 463 { 464 if(result==T7.getText()) 465 { 466 T8.setText("答案正确!"); 467 } 468 else 469 T8.setText("答案错误!正确答案是"+result); 470 dadui++; 471 } 472 }break; 473 case 3: 474 { 475 476 int shu1=(int) (Math.random()*9+1); 477 int res1=shu1; 478 T6.setText(String.valueOf("!"+shu1)); 479 for(int i=shu1-1;i>0;i--) 480 { 481 res1=res1*i; 482 } 483 result=String.valueOf(res1); 484 if(e.getSource()==B3) 485 { 486 if(result==T7.getText()) 487 { 488 T8.setText("答案正确!"); 489 } 490 else 491 T8.setText("答案错误!正确答案是"+result); 492 dadui++; 493 } 494 }break; 495 496 } 497 498 }break; 499 case 4: 500 { 501 int leixing=(int) (Math.random()*8/3+1);//1表示整数,2表示分数,3表示阶乘; 502 switch(leixing) 503 { 504 case 1: 505 { 506 int shu1=(int) (Math.random()*10+1); 507 int shu2=(int) (Math.random()*10+1); 508 T4.setText(String.valueOf(shu1)); 509 T6.setText(String.valueOf(shu2)); 510 T5.setText(String.valueOf("/")); 511 result=String.valueOf(shu1/shu2); 512 if(e.getSource()==B3) 513 { 514 if(result==T7.getText()) 515 { 516 T8.setText("答案正确!"); 517 } 518 else 519 T8.setText("答案错误!正确答案是"+result); 520 dadui++; 521 } 522 }break; 523 case 2: 524 { 525 int shu1=(int) (Math.random()*10+1); 526 int shu2=(int) (Math.random()*10+1); 527 int shu3=(int) (Math.random()*10+1); 528 int shu4=(int) (Math.random()*10+1); 529 if(shu1>shu2) 530 { 531 int temp=shu1; 532 shu1=shu2; 533 shu2=temp; 534 } 535 if(shu3>shu4) 536 { 537 int temp=shu3; 538 shu3=shu4; 539 shu4=temp; 540 } 541 542 T4.setText(String.valueOf(shu1+"/"+shu2)); 543 T6.setText(String.valueOf(shu3+"/"+shu4)); 544 T5.setText(String.valueOf("/")); 545 int res1=shu1*shu4; 546 int res2=shu2*shu3; 547 int pan=0; 548 if(res1>res2) 549 { 550 pan=res2; 551 } 552 else 553 { 554 pan=res1; 555 } 556 for(int i=pan;i>0;i--) 557 { 558 if(res1%pan==0&&res2%pan==0) 559 { 560 res1=res1/pan; 561 res2=res2/pan; 562 if(res1>res2) 563 { 564 pan=res2; 565 } 566 else 567 { 568 pan=res1; 569 } 570 i=pan; 571 } 572 } 573 574 result=String.valueOf(shu1+"/"+shu2); 575 if(e.getSource()==B3) 576 { 577 if(result==T7.getText()) 578 { 579 T8.setText("答案正确!"); 580 } 581 else 582 T8.setText("答案错误!正确答案是"+result); 583 dadui++; 584 } 585 }break; 586 case 3: 587 { 588 589 int shu1=(int) (Math.random()*9+1); 590 int res1=shu1; 591 T6.setText(String.valueOf("!"+shu1)); 592 for(int i=shu1-1;i>0;i--) 593 { 594 res1=res1*i; 595 } 596 result=String.valueOf(res1); 597 if(e.getSource()==B3) 598 { 599 if(result==T7.getText()) 600 { 601 T8.setText("答案正确!"); 602 } 603 else 604 T8.setText("答案错误!正确答案是"+result); 605 dadui++; 606 } 607 }break; 608 609 } 610 611 }break; 612 } 613 614 } 615 616 617 618 } 619 620 621 622 623 624 625 public static void main(String[] args) { 626 // TODO Auto-generated method stub 627 new Jiemian(); 628 629 630 } 631 632 } 633 634 心得:这个作业较难,遇到很多问题,程序还不够完善。 635 同队的小伙伴:软一46号杨灵超 http://www.cnblogs.com/Lingchao/
标签:
原文地址:http://www.cnblogs.com/chenjianding/p/4423391.html