如何检查数字是否与if语句vb.net匹配

时间:2013-09-06 06:17:25

标签: vb.net

请您告诉我如何解决当前问题。我不确定如何将其付诸实践。

我有一个计数器,它在for语句中增加了一个 我想添加一个需要执行以下操作的if语句:

Dim count as decimal = 1
For i As Integer = 1 To 400 - 1
   If count = 3 or count = 6 or count = 9 or count = 12 ..and on and on
       'All the numbers that mathes the count
   Else
       'All the numbers that does not match
   End if

   count += 1
Next

我想要一个simpaler方法来编写If count = 3或count = 6等等

4 个答案:

答案 0 :(得分:2)

如果计数应该可以分为3而不休息(看起来似乎是这种情况),您可以使用Mod运算符:Documentation

Mod运算符会将2个数字分开并返回剩余数字,因此14 Mod 3将为2。因此,您需要做的唯一检查是count Mod 3 = 0如果:

Dim count as decimal = 1
For i As Integer = 1 To 400 - 1
   If count Mod 3 = 0 then
       'All the numbers that mathes the count
   Else
       'All the numbers that does not match
   End if

   count += 1
Next

答案 1 :(得分:2)

1)为什么icount似乎总是相同的值?

2)两种可能的解决方案:正如其他人已经注意到的Mod运算符,假设您确实需要每三个数字,或者:

For i As Integer = 1 To 400 - 1
   Select Case i
       Case 3,6,9,12,15....
           'Do stuff here for matching
       Case Else
           'All the numbers that does not match
   End Select
Next

答案 2 :(得分:1)

模数是你的朋友。

number1 Mod number2


if count MOD 3 = 0

http://msdn.microsoft.com/en-us/library/se0w9esz(v=vs.90).aspx

答案 3 :(得分:1)

我不确定语法,但您需要使用Mod运算符:

Dim count as decimal = 1
For i As Integer = 1 To 400 - 1
   If (count Mod 3) = 0
       'All the numbers that mathes the count
   Else
       'All the numbers that does not match
   End if

   count += 1
Next