Four arithmetic simulation software in Java

Source: Internet
Author: User
Code:
  1. Import java. Applet .*;
  2. Import java. AWT .*;
  3. Import java. AWT. event .*;
  4. Public class game extends applet implements actionlistener {
  5. // Define variables
  6. Int num1, num2, num3, jieguo, jilu, error = 0, Right = 0, key = 0;
  7. Char op;
  8. Float percent = 0;
  9. String res, lev;
  10. Image labmag;
  11. Audioclip audio1, audio2;
  12. Label prompt1, prompt2;
  13. Textfield QUS, ANS, Info;
  14. Button next, enter, record;
  15. Public void Init ()
  16. {
  17. // Initialize the image
  18. Labmag = getimage (getcodebase (), "1.jpg ");
  19. // Initialize music
  20. Audio1 = getaudioclip (getdocumentbase (), "error. Au ");
  21. Audio2 = getaudioclip (getdocumentbase (), "Right. Au ");
  22. QUS = new textfield (15 );
  23. // Initialization button, text box
  24. Ans = new textfield (10 );
  25. Info = new textfield (30 );
  26. Enter = new button ("OK ");
  27. Next = new button ("new question ");
  28. Record = new button ("record ");
  29. Prompt1 = new label ("question :");
  30. Prompt2 = new label ("Result :");
  31. // Create button and text box
  32. Add (prompt1 );
  33. Add (QUS );
  34. Add (ANS );
  35. Add (enter );
  36. Add (next );
  37. Add (record );
  38. Add (prompt2 );
  39. Add (Info );
  40. // Listen to the three buttons
  41. Next. addactionlistener (this );
  42. Enter. addactionlistener (this );
  43. Record. addactionlistener (this );
  44. }
  45. Public void paint (Graphics g)
  46. {
  47. // Drawing
  48. G. drawimage (labmag, 0, 0, 430,280, this );
  49. }
  50. Public void actionreceivmed (actionevent E)
  51. {
  52. If (E. getsource () = NEXT)
  53. {
  54. // Random Number Generation
  55. Num1 = (INT) (math. Random () * 9 );
  56. Num2 = (INT) (math. Random () * 9 );
  57. Num3 = (INT) (math. Random () * 3 );
  58. Switch (num3)
  59. {
  60. Case 0: op = '+'; jilu = num1 + num2; break;
  61. Case 1: op = '-'; jilu = num1-num2; break;
  62. Case 2: op = '*'; jilu = num1 * num2; break;
  63. }
  64. // Considering the decimal point problem caused by integer division, Division generation is temporarily canceled.
  65. QUS. settext ("" + num1 + OP + num2 + "= ");
  66. Key = 1;
  67. }
  68. If (E. getsource () = enter)
  69. {
  70. If (ANS. gettext ()! = "")
  71. Jieguo = integer. parseint (ANS. gettext ());
  72. If (jieguo = jilu)
  73. {
  74. Res = "awesome, correct! ";
  75. Audio2.play (); // play music
  76. If (Key = 1) // only one point is recorded
  77. {
  78. Right ++;
  79. Key = 0;
  80. }
  81. }
  82. Else
  83. {
  84. Res = "incorrect. Don't be discouraged! ";
  85. Audio1.play (); // play music
  86. Error ++; // points are deducted if an error is returned.
  87. }
  88. Info. settext ("" + Res );
  89. }
  90. If (E. getsource () = record)
  91. {
  92. // Level Determination
  93. If (Right> 0 & right <= 10)
  94. Ev = "lazy goat ";
  95. If (Right> 10 & right <= 23)
  96. Ev = "warm goat ";
  97. If (Right> 23 & right <= 38)
  98. Ev = "Boiling goat ";
  99. If (Right> 38 & right <= 56)
  100. Ev = "meiyang ";
  101. If (Right> 56 & right <= 76)
  102. Ev = "Xi mianmian ";
  103. If (Right> 76 & right <= 100)
  104. Ev = "soft ";
  105. If (Right> 100)
  106. Ev = "Tian Shen ";
  107. Info. settext ("correct" + right + "Question" + "wrong" + error + "Question" + "grade:" + lev.com );
  108. }
  109. }
  110. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.