对于循环vb.net增加

时间:2014-04-06 15:49:59

标签: vb.net for-loop

我有一个小问题。如果我做了这样的for循环,如果除法的余数为0,则将myvar值增加30。

  Dim myvar As Integer = 49
        For i As Integer = 0 To myvar
            Console.WriteLine("Pasada " & i)
            If Math.IEEERemainder(i, 3) = 0 Then
                myvar += 30
            End If
        Next

如何将重复更改为myvar的新值?

2 个答案:

答案 0 :(得分:2)

您无法更改For循环的边界(出于好的理由),因此您必须使用手动循环替换For

Dim myvar = 49
Dim i = 0

While i <= myvar
    Console.WriteLine("Pasada " & i)

    If i Mod 3 = 0 Then
        myvar += 30
    End If

    i += 1
End While

答案 1 :(得分:2)

为什么不使用Do Loop

Dim myvar As Integer = 49
Dim i As Integer
    Do While i <= myvar
        Console.WriteLine("Pasada " & i)
        If Math.IEEERemainder(i, 3) = 0 Then
            myvar += 30
        End If
        i +=1
    Loop