Packagesz;ImportJava.util.*;Importjava.applet.*;//Introducing the Applet classImportjava.awt.*; Importjava.awt.event.*;//Introduce the System event class package. Public classV11extendsAppletImplementsactionlistener{//defines the class and implements the action event listener interface. Label Label1,label2,label3,label4;//declares four label objects. TextField TF1;//Declaring a text box objectTextArea area;//declares a text area objectButton Bt1,bt2;//declaring two Button objects Doublelb2=0.0; intS=0; intA=0; intB=0; intY=0; intZ=0;//s saves the total number of questions, a saves the correct number of questions, and B saves the wrong number of answers. Public voidInit () {//initialized. label1=NewLabel ();//create an object that does not display content. Label2=NewLabel (); Label3=NewLabel (); Label4=NewLabel ("=");//create an object that displays the content as "=". tf1=NewTextField (8); Area=NewTextArea (13,40);//Create a text area of 13 rows and 40 columns. bt1=NewButton ("scoring");//Create a button that displays the title "Scoring". Bt2=NewButton ("Redo"); Add (Label1); Add (Label2); Add (LABEL3); Add (LABEL4); Add (TF1); Add (area); Add (BT1); Add (BT2); This. setSize (400, 150); //use sequential layouts to add components. Tf1.addactionlistener ( This); Bt1.addactionlistener ( This); Bt2.addactionlistener ( This);//Register this as the Click event Listener for FT1, BT1, BT2. Label1.settext (String.valueof (int) (Math.random () *10))); Label3.settext (string.valueof (int) (Math.random () *10));//randomly generate two or 10 integers and display them on labels 1 and 3, respectively. Lb2= (Math.random ());//randomly generates a decimal within a 0~1. if(Lb2>=0 && lb2<=0.25) Label2.settext ("+"); Else if(lb2>0.25 && lb2<=0.5) Label2.settext ("-"); Else if(lb2>0.5 && lb2<=0.75) Label2.settext ("*"); Else if(lb2>0.75 && Lb2<=1) Label2.settext ("/"); } //The IF statement is used to control the random display of "+,-, *,/" four operators. Public voidactionperformed (ActionEvent e) {//implements the event interface method. if(E.getsource () ==TF1) {//determine if the event source is TF1 or BT1 or BT2. s=s+1;//One trigger TF1 total number of questions added 1. if(Label2.gettext () = = "+"){ intX=integer.parseint (Label1.gettext ()) +Integer.parseint (Label3.gettext ());//The characters are converted into numeric values. if(x==Integer.parseint (Tf1.gettext ())) a=a+1;//If the text box is entered correctly, the number of correct answers is plus one. Elseb=b+1;//If the text box entered an error, add one to the wrong number. } Else if(Label2.gettext () = = "-"){ intX=integer.parseint (Label1.gettext ())-Integer.parseint (Label3.gettext ()); if(x==Integer.parseint (Tf1.gettext ())) a=a+1; Elseb=b+1; } Else if(Label2.gettext () = = "*"){ intX=integer.parseint (Label1.gettext ()) *Integer.parseint (Label3.gettext ()); if(x==Integer.parseint (Tf1.gettext ())) a=a+1; Elseb=b+1; } Else if(Label2.gettext () = = "/") {y=Integer.parseint (Label1.gettext ()); Z=Integer.parseint (Label3.gettext ()); Do{label3.settext (string.valueof (int) (Math.random () *10))); Z=Integer.parseint (Label3.gettext ()); } while(z==0);//The dividend is not 0 with the loop guarantee. DoubleX=double.parsedouble (Label1.gettext ())/Integer.parseint (Label3.gettext ()); if(Math.Round (x) = = (Integer.parseint (Tf1.gettext ())))//Ensure that two numbers are divided and rounded up again. A=a+1; Elseb=b+1; } label1.settext (String.valueof (int) (Math.random () *10))); Label3.settext (string.valueof (int) (Math.random () *10))); LB2=Math.random (); if(Lb2>=0 && lb2<=0.25) Label2.settext ("+"); Else if(lb2>0.25 && lb2<=0.5) Label2.settext ("-"); Else if(lb2>0.5 && lb2<=0.75) Label2.settext ("*"); Else if(lb2>0.75 && Lb2<=1) Label2.settext ("/"); Tf1.settext (""); } Else if(E.getsource () ==bt1) {//bt1 button click event. Area.settext ("");//clears the text area. Area.append ("*********************************************" + "\ n");Area.append ("A total Answer:" +s+ "problem.) "+" \ n "); Area.append ("Congratulations to you!" Answer correctly: "+a+" problem, Answer wrong: "+b+" problem. "+" \ n "); Area.append ("_____________________________________________" + "\ n"); Area.append ("Your current score is:" + ((a*100)/s));//add text to the text area. } Else if(E.getsource () ==bt2) {//BT2 button click event. Area.settext (""); Label1.settext (string.valueof (int) (Math.random () *10))); Label3.settext (string.valueof (int) (Math.random () *10))); LB2=Math.random (); if(Lb2>=0 && lb2<=0.25) Label2.settext ("+"); Else if(lb2>0.25 && lb2<=0.5) Label2.settext ("-"); Else if(lb2>0.5 && lb2<=0.75) Label2.settext ("*"); Else if(lb2>0.75 && Lb2<=1) Label2.settext ("/"); Tf1.settext (""); } } }
Huang Weijun |
http://www.cnblogs.com/hwj23/ |
Liuheng |
http://home.cnblogs.com/u/iliuh/ |
Testing and Encapsulation 5.2