Experiment 24 is a pair of operation pairs

Source: Internet
Author: User

Debug Environment: Eclipse

Code:

Package sz;
Import java.util.*;
Import java.applet.*; Introducing the Applet class
Import java.awt.*;
Import java.awt.event.*; Introduce the System event class package.
The public class V11 extends Applet implements actionlistener{//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 object
TextArea area; Declares a text area object
Button Bt1,bt2; declaring two Button objects
Double lb2=0.0;
int s=0;
int a=0;
int b=0;
int y=0;
int z=0;
s saves the total number of questions, a saves the correct number of questions, and B saves the wrong number of answers.

public void init () {//Initialize.
Label1=new Label (); Create an object that does not display content.
Label2=new Label ();
Label3=new Label ();
Label4=new Label ("="); Create an object that displays the content as "=".
Tf1=new TextField (8);
Area=new TextArea (13,40);//Create a text area of 13 rows and 40 columns.
Bt1=new button ("scoring"); Create a button that displays the title "Scoring".
Bt2=new button ("Redo");
Add (Label1);
Add (Label2);
Add (LABEL3);
Add (LABEL4);
Add (TF1);
Add (area);
Add (BT1);
Add (BT2); 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, 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 void actionperformed (ActionEvent e) {//Implements the event interface method.
if (E.getsource () ==TF1) {//Determines whether the event source is TF1 or BT1 or BT2.
s=s+1; One trigger TF1 total number of questions added 1.
if (label2.gettext () = = "+") {
int X=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.
Else
b=b+1; If the text box entered an error, add one to the wrong number.
}
else if (label2.gettext () = = "-") {
int X=integer.parseint (Label1.gettext ())-integer.parseint (Label3.gettext ());
if (X==integer.parseint (Tf1.gettext ()))
a=a+1;
Else
b=b+1;
}
else if (label2.gettext () = = "*") {
int X=integer.parseint (Label1.gettext ()) *integer.parseint (Label3.gettext ());
if (X==integer.parseint (Tf1.gettext ()))
a=a+1;
Else
b=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.
Double x=double.parsedouble (Label1.gettext ())/integer.parseint (Label3.gettext ());
if (Math.Round (x) = = (Integer.parseint (Tf1.gettext ()))//guaranteed to divide by two number, rounding and re-rounding.
a=a+1;
Else
b=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! 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 ("");
}
}
}

33 Liu Heng Blog: http://www.cnblogs.com/iliuh/

Experiment 24 is a pair of operation pairs

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.