在Visual Basic中查找多个最大值

时间:2012-02-07 00:10:25

标签: vba max

我已经在visual basic中编写了一个程序,并希望它显示输入到数组中的最大值。有时相同的值将被输入2次或更多次,我需要一些方法来检测这个并在程序结束时通知用户。

目前我正在执行find max,然后对数组项进行线性搜索。如果项目是=到max,那么我将数组boolean标志设置为true。我唯一的问题是显示多重最大值,因为我需要使用布尔数组中的位置来检测值数组中哪些项目出现的最大次数相等,如果这是有意义的。是否可以执行MsgBox(“...”)的固定循环而不重复文本,只需更改i的值(正在访问的值数组中的位置)。

提前致谢。

编辑
评论代码

max=0 
for i = 1 to 4 
if number(i)>number(max) then max=i 
next 

for i = 0 to 4 
if number(i)=number(max) then flag(i)=true 
next 
Msgbox("the biggest number you entered was" & number(max))

1 个答案:

答案 0 :(得分:0)

未经测试......

Dim n as Long

For i = 0 to 4
    If number(i) = number(max) Then
         n = n + 1
    End If
Next i

Dim msg As String

msg = "max = " & number(max)
If n > 1 Then
    msg = msg & " which appeared " & n & " times"
End If

MsgBox msg
相关问题