In the evening, Dalong brought a VB tutorial. I read it out of curiosity.

Source: Internet
Author: User
What I wroteCodeIt's much better than the book.

Analysis: to reduce the randomness of input and question content, the operands and operators use random functions.RNDProduced. The operand range is1 ~ 10Operator1 ~ 4Representatives respectively+,-,*,/.RNDThe number is0 ~ 1The real number1 ~ 10As the operand, you can useINT (10 * RND + 1)Expression implementation. The same is true for operators.

In this example5Controls, where tag controls1, Text box1, Command button2, Graphical box1Controls:

Default Control name

Control name

Title

Text 

Graphics

 Description

Name

Caption

Text

Picture

Label1

Lblexp

Blank

No definition

No definition

Show questions generated

Text1

Txtinput

No definition

Blank

No definition

Command1

Cmdok

OK

No definition

No definition

End of each question

Command2

Cmdmark

Scoring

No definition

No definition

Last score

Picture1

Picture1

No definition

No definition

Blank

Show questions and results

In this example, there are three event processes. To facilitate data sharing during the event process, form-level variables are defined before the event process:

Dim num1 as integer, num2 as integer'Stores two operands

Dim sexp as string

Dim result as single'Calculation Result

Dim NOK as integer, nerror as integer'Statistics calculation correctness and error

In this example, three event processes are used:

(1)Generate questions by generating random numbers

Private sub form_load ()

Dim NOP as integer, OP as string * 1'Storage operators

Num1 = int (10 * RND + 1 )'Generate1 ~ 10Operands

Num2 = int (10 * RND + 1 )'Generate1 ~ 10Operands

NOP = int (4 * RND + 1 )'Generate1 ~ 4Between Operators

Select case nop'Set1 ~ 4Convert the operator code to an operator, and perform four arithmetic operations.

Case 1

OP = "+": Result = num1 + num2

Case 2

OP = "-": Result = num1-num2

Case 3

OP = "*": Result = num1 * num2

Case 4

OP = "/": Result = num1/num2

End select

Sexp = num1 & OP & num2 & "="

Lblexp = sexp'Show generated question

End sub

(2)InTxtinputEnter the calculation result in the text box, and click "OK" to check whether the calculation result is correct in the graphic box.

Private sub cmdok_click ()

If Val (txtinput) = Result then'The input result is the same as that calculated by the computer.

Picture1.print sexp; txtinput; tab (10); "Y "'Display correct symbolsY

NOK = NOK + 1'Correct cumulative calculation count

Else

Picture1.print sexp; txtinput; tab (10); "X "'Show calculation error symbolsX

Nerror = nerror + 1'Cumulative calculation error count

End if

Txtinput = ""

Form_load'Next question generation

End sub

(3)Calculated score

Private sub register mark_click ()

Picture1.print "____________"

Picture1.print"Total computing"& Int (NOK + nerror )&"Question"

Picture1.print"Score"& Int (NOK/(NOK + nerror) * 100)

End sub

Dim X %, Y %, P %

Dim A (0 to 10) as integer

Dim B (0 to 10) as integer

Dim S (1 to 10) as integer

Private sub form_load ()

For I = 1 to 10

Label1.item (I). Caption = ""

Label2.item (I). Caption = ""

Label3.item (I). Caption = ""

Label4.item (I). Caption = ""

Text1.item (I). Visible = false

Command1.visible = false

Label5.item (I). Visible = false

Next

End sub

Private sub command2_click ()

Randomize

For I = 1 to 10

S (I) = int (4 * RND) + 1

X = int (9 * RND) + 1

Y = int (9 * RND) + 1

Select case s (I)

Case 1

A (I) = x

B (I) = y

Label2.item (I). Caption = "+"

Case 2

X = int (9 * RND) + 1

Y = int (9 * RND) + 1

A (I) = x

B (I) = y

If a (I) <B (I) then

P = a (I)

A (I) = B (I)

B (I) = P

End if

Label2.item (I). Caption = "-"

Case 3

A (I) = x

B (I) = y

Label2.item (I). Caption = "*"

Case 4

A (I) = x

B (I) = y

If a (I) <B (I) then

P = a (I)

A (I) = B (I)

B (I) = P

End if

If a (I) mod B (I) <> 0 then

I = I-1

Else

Label2.item (I). Caption = "/"

End if

End select

Label1.item (I). Caption = a (I)

Label3.item (I). Caption = B (I)

Label4.item (I). Caption = "="

Text1.item (I). Text = ""

Text1.item (I). Visible = true

Next

Command2.visible = false

Command1.visible = true

End sub

Private sub commandementclick ()

Dim U %

Dim Q (1 to 10) as integer

Dim r (1 to 10) as integer

For I = 1 to 10

If text1.item (I). Text <> "then

'If CINT (text1.item (I). Text) = ture then

R (I) = CINT (text1.item (I). Text)

Else

R (I) = 26

End if

Select case s (I)

Case 1

Q (I) = a (I) + B (I)

Case 2

Q (I) = a (I)-B (I)

Case 3

Q (I) = a (I) * B (I)

Case 4

Q (I) = a (I)/B (I)

End select

Label5.item (I). Visible = true

If R (I) = Q (I) then

U = u + 10

Label5.item (I). Caption ="Correct"

Else

Label5.item (I). Caption ="Error"

End if

Next

Label6.visible = true

Label6.caption ="Your score"+ CSTR (u) +"Minute"

End sub

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.