用VB编写一个求三个数中最大值MAX和最小值MIN的过程,然后用这个过程分别求三个数,五个数,七个数中的最大值和最小值。
答案:3 悬赏:10 手机版
解决时间 2021-05-09 13:00
- 提问者网友:姑娘长的好罪过
- 2021-05-08 19:41
哪位高手帮帮忙。
最佳答案
- 五星知识达人网友:詩光轨車
- 2021-05-08 21:19
Private Sub Form_Load()
Dim a(2) As Single
Dim b(4) As Single
Dim c(6) As Single
a(0) = InputBox("请输入第一个数(共3个):")
b(0) = a(0)
c(0) = a(0)
a(1) = InputBox("请输入第二个数(共3个):")
b(1) = a(1)
c(1) = a(1)
a(2) = InputBox("请输入第三个数(共3个):")
b(2) = a(2)
c(2) = a(2)
MsgBox "这三个数中的最大数是:" & Max(a) & ",这三个数中的最小数是:" & Min(a)
b(3) = InputBox("请输入第四个数(共5个):")
c(3) = b(3)
b(4) = InputBox("请输入第五个数(共5个):")
c(4) = b(4)
MsgBox "这五个数中的最大数是:" & Max(b) & ",这五个数中的最小数是:" & Min(b)
c(5) = InputBox("请输入第六个数(共6个):")
c(6) = InputBox("请输入第七个数(共7个):")
MsgBox "这七个数中的最大数是:" & Max(c) & ",这七个数中的最小数是:" & Min(c)
End Sub
Function Max(ByRef a() As Single) As Single '比较一些数找出最大数
Dim i As Integer
Dim b As Single
b = a(0)
For i = 1 To UBound(a)
If b < a(i) Then
b = a(i)
End If
Next
Max = b
End Function
Function Min(ByRef a() As Single) As Single '比较一些数找出最小数
Dim i As Integer
Dim b As Single
b = a(0)
For i = 1 To UBound(a)
If b > a(i) Then
b = a(i)
End If
Next
Min = b
End Function
把代码直接复制到窗体中测试即可
Dim a(2) As Single
Dim b(4) As Single
Dim c(6) As Single
a(0) = InputBox("请输入第一个数(共3个):")
b(0) = a(0)
c(0) = a(0)
a(1) = InputBox("请输入第二个数(共3个):")
b(1) = a(1)
c(1) = a(1)
a(2) = InputBox("请输入第三个数(共3个):")
b(2) = a(2)
c(2) = a(2)
MsgBox "这三个数中的最大数是:" & Max(a) & ",这三个数中的最小数是:" & Min(a)
b(3) = InputBox("请输入第四个数(共5个):")
c(3) = b(3)
b(4) = InputBox("请输入第五个数(共5个):")
c(4) = b(4)
MsgBox "这五个数中的最大数是:" & Max(b) & ",这五个数中的最小数是:" & Min(b)
c(5) = InputBox("请输入第六个数(共6个):")
c(6) = InputBox("请输入第七个数(共7个):")
MsgBox "这七个数中的最大数是:" & Max(c) & ",这七个数中的最小数是:" & Min(c)
End Sub
Function Max(ByRef a() As Single) As Single '比较一些数找出最大数
Dim i As Integer
Dim b As Single
b = a(0)
For i = 1 To UBound(a)
If b < a(i) Then
b = a(i)
End If
Next
Max = b
End Function
Function Min(ByRef a() As Single) As Single '比较一些数找出最小数
Dim i As Integer
Dim b As Single
b = a(0)
For i = 1 To UBound(a)
If b > a(i) Then
b = a(i)
End If
Next
Min = b
End Function
把代码直接复制到窗体中测试即可
全部回答
- 1楼网友:洒脱疯子
- 2021-05-08 23:45
input "n=";n dim a[n] as integer for i=1 to n input a[i] next i
dim max as integer
dim min as integer max=a[1]
mix=a[1] for i=2 to n if max<a[i] then max=a[i] next i
for i=2 to n if min>a[i] then min=a[i] next i print "最大值为:" & max & ",最小值:" & min end
这个可以根据你输入的个数,再输入对应的值求出最大值和最小值
- 2楼网友:第幾種人
- 2021-05-08 22:07
Function Max1(val1,val2)
if val1>val2 then Max1=val1 else Max1=val2
end function
Function Min1(val1,val2)
if val1>val2 then Min1=val2 else Min1=val1
end function
不管比较多少个数,都是从两个数比较做起
可以迭代这个函数即可
s1=Max1(max1(34,23),47)
即为求三个数的最大数
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯