Import Java.awt.borderlayout;import java.awt.color;import Java.awt.font;import Java.awt.gridbagconstraints;import Java.awt.gridbaglayout;import Java.awt.insets;import Java.math.roundingmode;import Java.util.Random;import Java.util.regex.pattern;import Javax.swing.jbutton;import Javax.swing.jeditorpane;import Javax.swing.JFrame; Import Javax.swing.jlabel;import javax.swing.joptionpane;import javax.swing.jpanel;import Javax.swing.JTextField; Import javax.swing.swingconstants;/* * Test.java * * Created on __date__, __time__ *//** * * @author __user__ */public cl The extends Javax.swing.JFrame {public string tmnum;//the number of questions entered by public int n=0;//actually took to calculate the number of topics public string s[ ] = new STRING[5]; Pattern pattern = Pattern.compile ("[0-9]*"); public int result[] = new INT[5]; Private JPanel panel; Static jtextfield[] TextField = new JTEXTFIELD[5]; Long StartTime; Long EndTime; Long time; JLabel Labelresult = new JLabel ("post-submission results are shown here"); /** creates new form Test */ Public Test () {Super ("arithmetic"); Getcontentpane (). SetBackground (New Color (255, 255, 0)); SetBackground (New Color (255, 255, 255)); Setforeground (New Color (255, 255, 255)); This.setsize (700, 600); This.setlocation (350, 50);//main window position this.setdefaultcloseoperation (jframe.exit_on_close); This.setresizable (TRUE); Tmnum = Joptionpane.showinputdialog ("Please enter the number of questions (the number of topics cannot be greater than 5):"); while (true) {if (Pattern.matcher (Tmnum). Matches ()) {//If you enter a number, execute n = integer.parseint (tmnum) ; if (n <= 0 | | n >= 6) {tmnum = Joptionpane.showinputdialog ("Input Error! (The number of topics cannot be greater than 5): "); } else {n = integer.parseint (tmnum); Break }} else {tmnum = Joptionpane.showinputdialog ("Input Error! (The number of topics cannot be greater than 5): "); } } ; Initcomponents (); }/** This method was called from within the conStructor to * initialize the form. * Warning:do not modify this code. The content of this method was * always regenerated by the Form Editor. *///gen-begin:initcomponents//<editor-fold defaultstate= "collapsed" desc= "Generated Code" > private void Initcomponents () {Timu (); panel = new JPanel (); Panel.setforeground (New Color (0, 0, 0)); Panel.setbackground (New Color (253, 245, 230)); Getcontentpane (). Add (Panel, borderlayout.center); GridBagLayout gbl_p1 = new GridBagLayout (); Gbl_p1.columnwidths = new int[] {0, 0, 0}; Gbl_p1.rowheights = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; Gbl_p1.columnweights = new double[] {0.0, 0.0, double.min_value}; Gbl_p1.rowweights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, double.min_value }; Panel.setlayout (GBL_P1); for (int i = 0; i < n; i++) {JLabel label = new JLabel (s[I]);//First title label Label.setbackground (New Color (144, 238, 144)); Label.setfont (New Font ("Batang", Font.plain, 24)); Label.sethorizontalalignment (Swingconstants.left); Gridbagconstraints Gbc_label = new Gridbagconstraints (); Gbc_label.anchor = Gridbagconstraints.south; Gbc_label.fill = Gridbagconstraints.horizontal; Gbc_label.insets = new Insets (0, 20, 20, 0); Gbc_label.gridx = 0; Gbc_label.gridy = i+1; Panel.add (label, Gbc_label); Textfield[i] = new JTextField (10); Gridbagconstraints Gbc_textfield = new Gridbagconstraints (); Gbc_textfield.anchor = Gridbagconstraints.south; Gbc_textfield.insets = new Insets (0, 0, 20, 10); Gbc_textfield.fill = Gridbagconstraints.horizontal; Gbc_textfield.gridx = 1; Gbc_textfield.gridy = i + 1; Textfield[i].setcolumns (10); Panel.add (Textfield[i], Gbc_textField); } labelresult.setbackground (New Color (144, 238, 144)); Labelresult.setfont (New Font ("Batang", Font.plain, 24)); Labelresult.sethorizontalalignment (Swingconstants.left); Gridbagconstraints Gbc_label = new Gridbagconstraints (); Gbc_label.anchor = Gridbagconstraints.south; Gbc_label.fill = Gridbagconstraints.horizontal; Gbc_label.insets = new Insets (0, 20, 20, 0); Gbc_label.gridx = 0; Gbc_label.gridy = 6; Panel.add (Labelresult, Gbc_label); JButton jButton1 = new JButton ("submit");//Submit button gridbagconstraints Gbc_btnnewbutton = new Gridbagconstraints (); Gbc_btnnewbutton.anchor = Gridbagconstraints.northwest; Gbc_btnnewbutton.insets = new Insets (0, 100, 20, 40); Gbc_btnnewbutton.gridx = 0; Gbc_btnnewbutton.gridy = 0; Panel.add (JButton1, Gbc_btnnewbutton); JButton jButton2 = new JButton ("exit");//exit button gridbagconstraints Gbc_button = new GridbagconstrainTS (); Gbc_button.insets = new Insets (0, 0, 20, 40); Gbc_button.anchor = Gridbagconstraints.northwest; Gbc_button.gridx = 1; Gbc_button.gridy = 0; Panel.add (JButton2, Gbc_button); JButton JButton3 = new JButton ("Skin Change"); Gridbagconstraints Gbc_hybutton = new Gridbagconstraints (); Gbc_hybutton.anchor = Gridbagconstraints.northwest; Gbc_hybutton.insets = new Insets (0, 0, 0, 5); Gbc_hybutton.gridx = 2; Gbc_hybutton.gridy = 0; Panel.add (JButton3, Gbc_hybutton); StartTime = System.currenttimemillis ();//Start Time setdefaultcloseoperation (Javax.swing.WindowConstants.EXIT_ON_CLOSE) ; Jbutton1.addactionlistener (New Java.awt.event.ActionListener () {public void actionperformed (JAVA.AWT.EVENT.ACT Ionevent evt) {jbutton1actionperformed (evt); } }); Jbutton2.addactionlistener (New Java.awt.event.ActionListener () {public void actionperformed (JAVA.awt.event.actionevent evt) {jbutton2actionperformed (evt); } }); Jbutton3.addactionlistener (New Java.awt.event.ActionListener () {public void actionperformed (JAVA.AWT.EVENT.ACT Ionevent evt) {jbutton3actionperformed (evt); } }); Pack (); } private void Jbutton3actionperformed (Java.awt.event.ActionEvent evt) {panel.setbackground (new Color (254, 235, 150)); } private void Jbutton2actionperformed (Java.awt.event.ActionEvent evt) {system.exit (0); } private void Jbutton1actionperformed (Java.awt.event.ActionEvent evt) {endTime = System.currenttimemillis (); Time = Endtime-starttime; int time1 = (int) (time/1000); String s = "Altogether:" +time1+ "s"; int count=0; for (int i = 0; i < n; i++) {String str = textfield[i].gettext (); String result = string.valueof (This.result[i]); if (Result.equaLS (str)) {count +=1; }} S + = "total correct" +count+ "problem"; Labelresult.settext (s); Labelresult.setfont (New Font ("imitation", Font.plain, 20)); } public void Timu () {//title char[] cal = {' + ', '-', ' * ', '/'}; Random RA = new Random (); int paramtype;//numeric type int topictype;//topic type int A; int b; char c; int result = 0; String str = new string (); for (int i = 0; i < n; i++) {Topictype = Ra.nextint (2); if (Topictype = = 0) {//normal arithmetic Paramtype = Ra.nextint (2); if (Paramtype = = 0) {//Negative a =-ra.nextint (100);//control random number value} else{//positive A = Ra.nextint (100);//control random number value} Paramtype = Ra.nextint (2); if (Paramtype = = 0) {//negative B =-ra.nextint (100);//control random number value} else{//positive b = ra.nextint (100);//control random number Value} int index = Ra.nextint (cal.length);//generate symbol, random number c = Cal[index]; if (a<0) {str + = "(" +a+ ")"; } else {str + = "" +a; } str + = C; if (b<0) {str + = "(" +b+ ") ="; } else {str + = b + "="; } s[i] = str; str = new String (); Switch (c) {//calculates the value of the random number case ' + ': {result = a + B; } break; Case '-': {result = a-B; } break; Case ' * ': {result = a * b; } break; Case '/': {result = A/b; } break; } This.result[i] = result; } else{int jiechengnum = Ra.nextint (6); S[i] = Jiechengnum + "! ="; int jiechengresult=1; if (Jiechengnum = = 0) {This.result[i] = 1; } else {for (int o = 1; o <= jiechengnum; o++) {Jiechengresult = Jiechengre Sult*o; } This.result[i]=jiechengresult; }}}}/** * @param args the command line arguments */public static void main (String Args[]) {java.awt.EventQueue.invokeLater (new Runnable () {public void run () {New Test () . setvisible (True); } }); }}
Development environment: Eclipse
Start interface: Allows the user to enter the number of questions.
The user enters the answer, at which point the system will calculate the number of questions answered and the time taken by the user.
Displays the time and correct number of questions.
This is the skin-changing interface.
Summarize:
After 2 days of hard work, we finally wrote an improved version of "arithmetic" under the conditions of reference and continuous testing.
This arithmetic completed the teacher's minimum requirements, and there are 4 extension directions: 1. UI skin Change, 2: User control of the number of questions, 3: Calculate user answer time; 4: Calculate how many questions the user answered correctly.
Each time the program is a test, not only require the students to write code to pay attention to detail, but also require teamwork spirit, when the companion card in a logical dilemma should be timely to tell the companion how is reasonable, so as to write a reasonable and useful code.
Partner:
Huang Weijun 201306114423 http://www.cnblogs.com/hwj23/
Chen Ruting 201306114454 http://i.cnblogs.com/EditPosts.aspx?opt=1
The improved arithmetic