Very practical java Automatic Answer timing splitter and java scoring

Source: Internet
Author: User

Very practical java Automatic Answer timing splitter and java scoring

This small program is written in java and allows users to calculate addition, subtraction, multiplication, division, or less than 10. It is especially suitable for students who begin to learn addition, subtraction, multiplication, division, and their computing capabilities, the gaming nature that has been scored and timing has aroused learning interest!

When you run this program, a window will pop up. Press enter to start answering questions. After the answer is finished, press enter to perform scoring and correctness judgment, and issue the next question! Every round of 10 questions, each question 10 points, automatic timing score after the answer, the whole operation only needs to press enter key, no other button, kindergarten primary school students will play!

In addition, a checksum prompt is provided for the input validity! You are welcome to make full use of your imagination!

Code:

Package autoScore; import java. awt. color; import java. awt. font; import java. awt. label; import java. awt. list; import java. awt. textField; import java. awt. event. actionEvent; import java. awt. event. keyEvent; import java. awt. event. keyListener; import java. text. decimalFormat; import javax. swing. JFrame; public class AutoScore extends JFrame {Label labRule = new Label (); // rule description Label labA = new Label (); // The first number Label labOp = new Label (); // Label labB = new Label (); // Label label5 = new Label (); // equal to sign "=" Label labWarn = new Label (); // enter the legality check prompt Label labQues = new Label (); // Answer list Label labResult = new Label (); // score tag TextField txtAnswer = new TextField (); // enter the answer input box int total = 0; // record the total number of answers int right = 0; // record the correct number of answers int error = 0; // record the number of answer errors int score = 0; // record the total answer score boolean isOver = false; // mark boolean isFirst = true after the end of a round; // Long startTime indicates the first running of the program; // the start time of each running round. List listDisp = new List (); // List listScore = new List (); // score display box public static void main (String [] args) {AutoScore score = new AutoScore ();} public AutoScore () {init (); setSize (450,630 ); setdefaclocloseoperation (EXIT_ON_CLOSE); setVisible (true);} public void init () {setLayout (null); setSize (450,630); labRule. setText ("rule: 10 questions in each group, 10 points for each question, press ENTER to start, and retain 2 decimal places"); la BRule. setBounds (36, 10,390, 72); labRule. setFont (new Font ("Dialog", Font. PLAIN, 12); getContentPane (). add (labRule); labA. setText ("x"); labA. setBounds (36,82, 36,36); labA. setFont (new Font ("Dialog", Font. PLAIN, 24); getContentPane (). add (labA); labOp. setText ("+"); labOp. setFont (new Font ("Dialog", Font. PLAIN, 24); labOp. setBounds (72, 82, 45, 36); getContentPane (). add (labOp); labB. setText ("y"); labB. setFo Nt (new Font ("Dialog", Font. PLAIN, 24); labB. setBounds (118, 82, 33, 36); getContentPane (). add (labB); label5.setText ("="); label5.setFont (new Font ("Dialog", Font. PLAIN, 24); label5.setBounds (168, 82, 24, 36); getContentPane (). add (label5); labWarn. setFont (new Font ("Dialog", Font. PLAIN, 12); labWarn. setBackground (Color. RED); labWarn. setBounds (320, 82, 80, 36); labWarn. setVisible (false); getContentPane (). Add (labWarn); labQues. setText ("Answer list:"); labQues. setFont (new Font ("Dialog", Font. PLAIN, 12); labQues. setBounds (36,148,100, 20); getContentPane (). add (labQues); labResult. setText ("score statistics:"); labResult. setFont (new Font ("Dialog", Font. PLAIN, 12); labResult. setBounds (36,420,100, 20); labResult. setVisible (false); getContentPane (). add (labResult); txtAnswer. setFont (new Font ("Dialog", Font. PLAIN, 24); txtAnswer. SetBounds (216, 82,100, 36); getContentPane (). add (txtAnswer); listDisp. setFont (new Font ("Dialog", Font. PLAIN, 16); listDisp. setBounds (36,174,282,230); getContentPane (). add (listDisp); listScore. setFont (new Font ("Dialog", Font. PLAIN, 16); listScore. setBounds (36,450,282,135); MyKey myKey = new MyKey (); // Answer input box, keyboard button listening class txtAnswer. addKeyListener (myKey);} class MyKey implements KeyListener {@ Override pu Blic void keyTyped (KeyEvent e) {}@ Override public void keyPressed (KeyEvent e) {if (e. getSource () = txtAnswer) {if (e. getKeyCode () = KeyEvent. VK_ENTER) {if (isOver | isFirst) {updateQuestion (null);} else if ("". equals (txtAnswer. getText () {labWarn. setText ("Enter the answer! "); LabWarn. setVisible (true);} else {labWarn. setVisible (false); if (! IsNumber (txtAnswer. getText () {labWarn. setText ("enter a number! "); LabWarn. setVisible (true);} else if (total <9) {judge (null); updateQuestion (null);} else {judge (null); labResult. setVisible (true); scoresponmed (null) ;}}}@ Override public void keyReleased (KeyEvent e) {}} int a = 0, B = 0; string op = ""; double result = 0; DecimalFormat df = new DecimalFormat ("#. 00 ");/*** question Method * @ param e */public void updateQuestion (ActionEvent e) {if (isFirst) {startTime = System. currentTimeMillis ();} if (isOver = true) {listDisp. clear (); listScore. clear (); labResult. setVisible (false); listScore. setVisible (false);} isOver = false; a = (int) (Math. random () * 9 + 1); B = (int) (Math. random () * 9 + 1); int c = (int) (Math. random () * 4); switch (c) {case 0: op = "+"; result = a + B; break; case 1: op = "-"; result = a-B; break; case 2: op = "*"; result = a * B; break; case 3: op = "/"; result = (a * 1.0)/B; // if the two numbers are not divided, two decimal places if (String. valueOf (result ). length ()> 10) {result = Double. parseDouble (df. format (a * 1.0)/B);} break;} labA. setText (String. valueOf (a); labB. setText (String. valueOf (B); labOp. setText (op); label5.setText ("="); txtAnswer. setText (""); isFirst = false;}/*** judgment result * @ param e */public void judge (ActionEvent e) {try {double value = Double. parseDouble (txtAnswer. getText (); String resultStr = (total + 1) + "," + a + op + B + "=" + value; if (value = result) {resultStr + = "\ t correct"; right ++; score + = 10;} else {resultStr + = "\ t incorrect answer:" + result; error ++;} listDisp. add (resultStr); total ++;} catch (NumberFormatException ignored) {}}/*** statistical score * @ param e */public void scoresponmed (ActionEvent e) {isOver = true; listScore. clear (); listScore. setVisible (true); String exitStr = "total answer" + total + ""; listScore. add (exitStr); listScore. add ("accumulative time:" + (System. currentTimeMillis ()-startTime)/1000 + "seconds"); listScore. add ("correct:" + right + "); listScore. add ("error:" + error + ""); listScore. add ("score:" + score + "score"); getContentPane (). add (listScore); score = 0; right = 0; error = 0; total = 0 ;} /*** check whether the input digit is a digit * @ param numberStr * @ return */public boolean isNumber (String numberStr) {boolean isNumber = true; try {Double. parseDouble (numberStr);} catch (NumberFormatException e) {isNumber = false;} return isNumber ;}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.