简体   繁体   中英

How to check if numbers match for if statement vb.net

Will you please give me a gide line on how to solve my current problem. I'm not sure how to put this in practice.

I have a counter which increase by one in a for statement I want to add a if statment that needs to do the following:

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

I want a simpaler method on how to write the If count = 3 or count = 6 and so on

If the count should be dividable by 3 without a rest (as it seems to be the case), you can use the Mod operator: Documentation

The Mod operator will divide 2 numbers and will return the remaining, so eg 14 Mod 3 will be 2 . So the only check, you need to do, is if count Mod 3 = 0 like:

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) Why do you have i and count which appear to always be the same value?

2) Two possible solutions: either the Mod operator as others have noted, assuming you actually want every third number, or:

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

Modulus is your friend.

number1 Mod number2


if count MOD 3 = 0

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

I'm not sure about syntax, but you need to use Mod operator:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM