我有以下数据:(id,volume,median.percantile)
1 Normal Normal Low
2 Low Normal High
3 High High Normal
我需要做的是让一个计数器在不正常时增加1。所以我希望
1 Normal Normal Low Counter:1
2 Low Normal High Counter:2
3 High High Low Counter:3
为了提供它,我写了下面的函数。但是,当我发送(正常,正常,高)时,它正在计算它2而不是1.所以它不能正常工作。我的错误在哪里?
我的代码如下:
Public Function getPoint(ByVal volume As String, ByVal median As String, ByVal percantile As String) As Integer
Dim Total As Integer = 0
If volume = “Normal” then
total = total + 1
end if
If median = “Normal” then
total = total + 1
end if
If percantile = “Normal” then
total = total + 1
end if
return total
End Function
答案 0 :(得分:1)
在第一条记录中,volume
和 median
均为"Normal"
。所以这些条件的两个都是真的:
If volume = "Normal" Then
total = total + 1
End If
If median = "Normal" Then
total = total + 1
End If
因此,total
增加两倍。如果您希望它只增加一次(并且基本上忽略剩余的比较),那么在条件满足后使用ElseIf
来停止比较:
If volume = "Normal" Then
total = total + 1
ElseIf median = "Normal" Then
total = total + 1
End If
或者只是把它们放在一个条件中:
If volume = "Normal" Or median = "Normal" Then
total = total + 1
End If
请注意,此行为与您所描述的完全相反:
不正常时增加1
也许错字?
答案 1 :(得分:0)
你所做的与你想要的相反。
您希望计数不正常,而您正在计算并返回总计是正常的。
简单修复将是return (3 - total)
。
答案 2 :(得分:0)
将"="
更改为"<>"
即。
If volume <> "Normal" then
total = total + 1
End if