VB编程题:从键盘输入三个数,要求按从小到大的顺序输出到窗体上
展开全部
可以采取两种方法:
'方法一,不是用数组,简单灵活
Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single, tmp As Single
a = Val(InputBox("请输入第 1 个数!", , 1))
b = Val(InputBox("请输入第 2 个数!", , 2))
c = Val(InputBox("请输入第 3 个数!", , 3))
Print "从小到大分别为 ";
If a > b Then
tmp = a
a = b
b = tmp
End If
If a > c Then
tmp = c
c = b
b = a
a = tmp
ElseIf b > c Then
tmp = b
b = c
c = tmp
End If
Print a; b; c
End Sub
'方法二,使用数组,代码紧促
Private Sub Command2_Click()
Dim a(2) As Single, tmp As Single
Dim i As Integer, j As Integer
For i = 0 To 2
a(i) = Val(InputBox("请输入第 " & i & " 个数!", , i))
Next i
For i = 0 To 1
For j = i + 1 To 2
If a(i) > a(j) Then
tmp = a(i)
a(i) = a(j)
a(j) = tmp
End If
Next
Next
Print "从小到大分别为 "; a(0); a(1); a(2)
End Sub
'方法一,不是用数组,简单灵活
Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single, tmp As Single
a = Val(InputBox("请输入第 1 个数!", , 1))
b = Val(InputBox("请输入第 2 个数!", , 2))
c = Val(InputBox("请输入第 3 个数!", , 3))
Print "从小到大分别为 ";
If a > b Then
tmp = a
a = b
b = tmp
End If
If a > c Then
tmp = c
c = b
b = a
a = tmp
ElseIf b > c Then
tmp = b
b = c
c = tmp
End If
Print a; b; c
End Sub
'方法二,使用数组,代码紧促
Private Sub Command2_Click()
Dim a(2) As Single, tmp As Single
Dim i As Integer, j As Integer
For i = 0 To 2
a(i) = Val(InputBox("请输入第 " & i & " 个数!", , i))
Next i
For i = 0 To 1
For j = i + 1 To 2
If a(i) > a(j) Then
tmp = a(i)
a(i) = a(j)
a(j) = tmp
End If
Next
Next
Print "从小到大分别为 "; a(0); a(1); a(2)
End Sub
追问
第一个方法好谢谢
追答
祝你成功!
展开全部
Private Sub Command1_Click()
Dim A(1 To 3) As Single, X As Single, I As Integer, J As Integer
For I = 1 To 3
A(I) = Val(InputBox("请输入第" & I & "个数!", "第" & I & "个数:", 0))
Next I
For I = 1 To 2
For J = 1 To 3 - I
If A(J) > A(J + 1) Then
X = A(J)
A(J) = A(J + 1)
A(J + 1) = X
End If
Next J
Next I
Print "最小数=" & A(1) & ",中间数=" & A(2) & ",最大数=" & A(3)
'或添加Text1控件
'Text1.Text = "最小数=" & A(1) & ",中间数=" & A(2) & ",最大数=" & A(3)
End Sub
Dim A(1 To 3) As Single, X As Single, I As Integer, J As Integer
For I = 1 To 3
A(I) = Val(InputBox("请输入第" & I & "个数!", "第" & I & "个数:", 0))
Next I
For I = 1 To 2
For J = 1 To 3 - I
If A(J) > A(J + 1) Then
X = A(J)
A(J) = A(J + 1)
A(J + 1) = X
End If
Next J
Next I
Print "最小数=" & A(1) & ",中间数=" & A(2) & ",最大数=" & A(3)
'或添加Text1控件
'Text1.Text = "最小数=" & A(1) & ",中间数=" & A(2) & ",最大数=" & A(3)
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询