How to use VB language to find the maximum value and average value
This article mainly introduces how to use VB language to find the maximum and average, the article is very detailed, has a certain reference value, interested friends must read it!
Text explanation:
Request:
1. The background color of the number changes randomly every 0.1 second.
2. The numbers are random in [10, 10, 90].
3. after the click is generated, the number begins to be obtained, and the text becomes stopped.
4. Find the maximum, minimum and average
Summary
1. When comparing, it must be numeric to compare, and the val () function should be used to convert it into numerical value.
2. Color function rgb (red, green, blue)
The range of values for each color is 0-255
3. Master the use of random numbers
4. in order to find the maximum value, you should learn to assume the maximum or minimum value.
5. Sum before finding the average
Software design interface:
Source code sharing:
Private Sub Command1_Click () If Command1.Caption = "generate" Then Timer1.Enabled = True Command1.Caption = "stop" Else Timer1.Enabled = False Command1.Caption = "generate" End IfEnd Sub
Private Sub Command2_Click () 'assumes that the values in the first label are minimum and maximum Dim min%, max%
Min = Val (Label1 (0) .Caption) max = Val (Label1 (0) .Caption)
'Loop comparison Dim i%For I = 1 To 17 'compare minimum value If min > Val (Label1 (I) .Caption) Then min = Val (Label1 (I) .Caption)' compare maximum value If max < Val (Label1 (I) .Caption) Then max = Val (Label1 (I) .Caption) Next
Label2.Caption = "maximum: & max &", minimum: "& min
End Sub' to find the average Private Sub Command3_Click ()
Dim sum&, I% 'find the sum of all worthy For I = 0 To 17 sum = sum + Val (Label1 (I) .caption) Next
Label2.Caption = "average:" & (sum / 17)
End Sub
Private Sub Timer1_Timer () Dim i%RandomizeFor I = 0 To 17 Step 1 'background color change Label1 (I). BackColor = RGB (Int (Rnd * 256), Int (Rnd * 256), Int (Rnd * 256)' integers Label1 (I) randomly generated between 10 and 90. Caption = Int (Rnd * 90) + 1 Next iEnd Sub
Screenshot of the running effect:
The above is all the contents of the article "how to use VB language to find the maximum and average value". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!