Visual Basic - 我的字符串比较功能无法正常工作

时间:2016-04-07 17:38:24

标签: vb.net

我有以下数据:(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

3 个答案:

答案 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)

不过,这是VBA还是VB.Net?两者都基于VB,但不同。 如果它是VBA for Excel,你可以使用简单的计数公式(3 - 正常计数)来实现这一点。

答案 2 :(得分:0)

"="更改为"<>"
即。

If volume <> "Normal" then 
    total = total + 1 
End if