⑴ VB編程:編寫程序,產生50個互不相同的10~99的隨機整數,統計各數值段有多少個數
Dim a(1 To 50) As Integer, b(1 To 9) As Integer
Dim i As Integer, j As Integer
Randomize
For i = 1 To 9
b(i) = 0 '初始化數組
Next
For i = 1 To 50
a(i) = Int(90 * Rnd + 10) '產生隨機數
For j = 1 To 9 '判定范圍存入數組
If a(i) > j * 10 - 1 And a(i) < j * 10 + 10 _
Then b(j) = b(j) + 1
Next
Next
'輸出
For i = 1 To 50
Debug.Print "數" & i & "=" & a(i)
Next
For j = 1 To 9
Debug.Print "范圍" & (j * 10) & "到" & (j * 10 + 9) & "的數有" & b(j) & "個。"
Next
⑵ VB編程,超簡單的。求編寫代碼
第一題:
Private Sub Form_Click()
Dim x As Double, Y As Double
x = InputBox("請輸入當前顧客購買雞蛋的重量(以公斤為單位)")
If x < 3 Then
Y = 7.6 * x
Else
Y = 7.6 * x * 0.8
End If
Print Format(Y, "0.00")
End Sub
第二題:
Private Sub Form_Click()
Dim x As Double, Y As Double
x = InputBox("請輸入顧客所乘的公里數")
If x / Fix(x) = 1 Then
x = x
Else
x = Fix(x) + 1
End If
If x > 15 Then
Y = 7 + 13 * 1.5 + (x - 15) * 2.1
ElseIf x > 2 Then
Y = 7 + (x - 2) * 1.5
Else
Y = 7
End If
Print Y
End Sub
⑶ 求VB中必須背過的命令
789+
456-
123*
0.=/
每個一個按鈕(每個按鈕對應數字),一個文本框。
一下是最基礎代碼(其實可以用按鈕數組)
----------------------
Dim d As Integer
Dim x As String
Dim y As String
Dim k As Integer
Private Sub Command1_Click(Index As Integer)
Label1.Caption = Label1.Caption + Command1(Index).Caption
End Sub
Private Sub Command2_Click()
y = Label1.Caption
If d = 0 Then
Label1.Caption = Val(x) + Val(y)
End If
If d = 1 Then
Label1.Caption = Val(x) - Val(y)
End If
If d = 2 Then
Label1.Caption = Val(x) * Val(y)
End If
If d = 3 Then
If Val(y) = 0 Then
Label1.Caption = "被除數不能為零"
Else
Label1.Caption = Val(x) / Val(y)
End If
End If
End Sub
Private Sub Command3_Click()
x = Label1.Caption
Label1.Caption = ""
d = 0
k = 0
End Sub
Private Sub Command4_Click()
x = Label1.Caption
Label1.Caption = ""
d = 1
k = 0
End Sub
Private Sub Command5_Click()
x = Label1.Caption
Label1.Caption = ""
d = 2
k = 0
End Sub
Private Sub Command6_Click()
x = Label1.Caption
Label1.Caption = ""
d = 3
k = 0
End Sub
Private Sub Command7_Click()
Label1.Caption = Sqr(Val(Label1.Caption))
End Sub
Private Sub Command8_Click()
Label1.Caption = ""
d = 9
k = 0
End Sub
Private Sub Command9_Click()
If k = 0 Then
If Label1.Caption = "" Then
Label1.Caption = Label1.Caption + Str(0) + Command9.Caption
k = k + 1
Else
Label1.Caption = Label1.Caption + Command9.Caption
k = k + 1
End If
End If
End Sub
⑷ VB編程的程序
'再窗體上放一個按鈕command1,一個圖形控制項image1,和一個滾動條HScroll1,代碼如下:
Dim W, H As Double
Private Sub Command1_Click() '按鈕單擊事件
Image1.Stretch = True
With HScroll1
.Enabled = True
.Min = 100
.Max = 2400
.LargeChange = 200
.SmallChange = 20
End With
End Sub
Private Sub Form_Load()
HScroll1.Enabled = False
W = Me.Image1.Width
H = Me.Image1.Height
End Sub
Private Sub HScroll1_Change()
Me.Image1.Width = W + Me.HScroll1.Value
Me.Image1.Height = H + Me.HScroll1.Value
End Sub