Experiment two----arithmetic--pair

Source: Internet
Author: User

Import Java.awt.*;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.util.Calendar ; Import Java.util.random;import Javax.swing.jbutton;import javax.swing.jframe;import Javax.swing.jlabel;import Javax.swing.joptionpane;import Javax.swing.jpasswordfield;import Javax.swing.jtextfield;import  Java.io.IOException;  Import Java.util.Timer;  public class SS {static int time1;static timer timer = new timer ();p ublic static void Main (string[] args) {JFrame JFrame = New JFrame ("arithmetic"); Jframe.setsize, Jframe.setdefaultcloseoperation (jframe.exit_on_close); Label L1=new label ("Generated Quantity:"), final label L3=new label (""), final label L4=new label (""), final label L5=new label ("Answer Time:"); f Inal label L2=new label ("Please enter Answer:"), final TextField t1= new TextField (""), final TextField t2= new TextField (""); final Textfi Eld t3= New TextField ("");  JButton B1 = new JButton ("Start generation");   JButton b2 = new JButton ("confirmation"); L1.setbounds ((), L2.setbounds (10,120, Max); L3.setbounds (40, L4.setbounds (40,140, Max.), L5.setbounds (150,20, Max), L3.setfont (New Font ("Arial", font.plain,20)); L4.setfont (New Font ("Arial", font.plain,20)),//l5.setfont (New Font ("Arial", font.plain,20)), T1.setbounds (80, 20, 50, 20); T2.setbounds (k, J, +), t3.setbounds (+----), b1.setbounds (+, +), b2.setbounds (+--); Frame.setlayout (null); Jframe.add (L1); Jframe.add (t1); Jframe.add (L2); Jframe.add (B1); Jframe.add (B2); Jframe.add (L3 ); Jframe.add (T2); Jframe.add (L4); Jframe.add (L5); Jframe.add (T3); jframe.setvisible (true); final C c=new C (); B1.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {Calendar Cld = Calendar . getinstance ();//c c=new C (integer.valueof (T1.gettext ())); if (Integer.valueof (T1.gettext ()) >5) { Joptionpane.showmessagedialog (NULL, "Number of topics greater than 5");} Else{//time1=cld.get (Calendar.hour) *3600+cld.get (calendar.minute) *60+cld.get (Calendar.second); System.out.print (T3.gettext ()), if (!t3.gettext (). Equals ("")) {Timer.schedule (newJava.util.TimerTask () {public void run () {Joptionpane.showmessagedialog (null, "Time is Over"), t1.settext (null); T2.settext ( NULL); L3.settext (null); L4.settext (null); T3.settext (null); Timer.cancel ();} },integer.valueof (T3.gettext ()) *1000);} L4.settext (null); C.initialize (Integer.valueof (T1.gettext ())); L3.settext (C.next ());}}); B2.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {int cuo;int dui; System.out.print (C.I.); System.out.print (Integer.valueof (T1.gettext ())); if (C.i<=integer.valueof (T1.gettext ()) &&c.j==0) {if ( C.check (Integer.valueof (T2.gettext ())) ==true) l4.settext ("correct"); Elsel4.settext ("Answer wrong" + "result is" + (int) c.d);   L3.settext (C.next ());}});}      public static void StopTime (Timer timer) {timer.cancel (); }}class c{static int max;static int i=0;static int j=0;int a[]=new int [6];int g;int c[]=new int [4]; String Res=new string (""); static double d=0;static int cuo=0;static int dui=0;static int time2; String Str=new string ("+-*/");p ublic C() {}public void initialize (int max) {I=0;this.max=max;} Public String Next () {if (I<max) {res= "", for (int k=0;k<5;k++) a[k]= (int) (Math.random () *100), for (int k=0;k<4;k + +) c[k]= (int) (Math.random ()); g= (int) (2+math.random ()); i++; for (int k=0;k<g;k++) System.out.print (A[k]); Boolean fu=false; int x; x= (int) (Math.random ()); for (int k=0;k<g;k++) if (k!=g-1) {if (x>=2) {a[k]=-a[k]; res=res+ "(" +a[k]+ ")" + "" +str.charat (C[k]);} else res=res+a[k]+ "" +str.charat (C[k]);} else res=res+a[k]+ ""; res=res+ "="; return res;} Calendar Cld1 = Calendar.getinstance (); Time2=cld1.get (Calendar.hour) *3600+cld1.get (calendar.minute) *60+Cld1.get ( Calendar.second)-ss.time1;//system.out.print (time2); Joptionpane.showmessagedialog (NULL, "Use Time" +time2); Time2=0;ss.stoptime (Ss.timer); return "Answer" +dui+ "+" + "wrong" + cuo+ "Problem"; System.out.print (A + "" +str.charat (c) + "" +b+ "=");} Public boolean check (double result) {double t1[]=new double[6];int y1[]=new int[5];int k=g,k2;int m;d=0;k2=g;int flag=0; for (int i=0;i<k;i++) t1[i]=a[i];for (int i=0;i<k-1;i++) Y1[i]=c[i];while (flag==0) {flag=1;for (int i=0;i<k;i++) {if ( y1[i]==2 | | y1[i]==3) {T1[i]=jisuan (t1[i],t1[i+1],y1[i]); for (m= (i+1); m<k-1;m++) {t1[m]=t1[m+1];} for (int n=i;n<k-1;n++) y1[n]=y1[n+1];k2--;flag=0;}}} System.out.print ("formula:" +t1[0]+y1[0]+ "" +t1[1]+y1[1]+ "" +t1[2]+y1[2]+ "" +t1[3]+y1[3]+ "" +t1[4]+y1[4] "); for (int i=0;i<k2;i++) if (y1[i]==1) t1[i+1]=0-t1[i+1];for (int i=0;i<k2;i++) d=d+t1[i]; System.out.println ("Answer" +d+ "k=" +k+ "k2=" +k2); if (result== (int) (d)) {Dui++;return true;} Else{cuo++;return false;}} Public double Jisuan (double a,double b,int ysf)//ysf operator {if (ysf==2) return A*b;else if (ysf==3) return (int) A/b;else if (YSF ==0) return a+b;else return a-B;}

  

Development environment: Eclipse

Pair Companion: 25 Liao Ho

Companion Blog: http://home.cnblogs.com/u/liaozhuoshen/

Implementation time: About 10 hours

Program Description:

Generate a single program with a user interface (not the console), a maximum of 4 operators per topic, operand -100--100.

If the user enters the correct answer, the prompt is correct, and if the answer is wrong, the error is indicated and the correct answer is indicated.

Division of labor in detail: altogether did 4 directions, Liao Ho do 3 directions, respectively is 3,4,7. I did 1 directions, 2, and the calculation of interface and four mixed operation

Direction of expansion:

1. after the user's answer is finished, the program can display the user's answer time

2. the user can choose the number of questions (maximum no more than 5 topics), the end of the answer can show the number of questions answered by the user and the number of correct questions

3. The program can set the answer time, the time is set to an integer, the unit is seconds, the maximum can not exceed 120 seconds, if the answer time is not answered, the prompt: Time has arrived, can not answer questions.

4. The expression contains a negative integer (negative integer minimum of less than 100), and negative numbers need to be parenthesized, the user input results without parentheses.

Operation Result:

Summary: I thought four mixed operation is relatively easy to complete, but when I do it, I found that this and I did not think so easy to achieve, in the implementation of this function encountered a lot of trouble, such as when encountering the multiplier method of the time also to consider their priority than addition and subtraction, so for this problem, I racked my brains, thinking a lot of ways, but finally ended up with a failure, and then I and teammate Liao Ho, came up with a way to use the loop with the array first multiplication method of the first operation, and then to subtract the final sum of the remaining results is the final answer. There is the basic knowledge of the interface, there are many have forgotten, at this time can only go through the book and Baidu solved. Through this experiment, I learned how to work with teammates to solve problems and learned a lot of knowledge

Experiment two----arithmetic--pair

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.