如何编写Visual Basic for-loop步增量?

时间:2015-02-19 14:27:36

标签: vb.net loops operators selection increment

我无法让我的项目工作。我需要显示一个工作时间表,每个第3个周末必须从第1周开始工作。假设一年的最后一周是52。

程序应该显示如下:

     Week 1         Week 4         Week 7
     Week 10        Week 13        Week 16
     Week 19        Week 22        Week 25
     Week 28        Week 31        Week 34
     Week 37        Week 40        Week 43
     Week 46        Week 49        Week 52

但是我的节目只播放了第4周

这是我的代码。我该如何解决这个问题?

If choice = 1 Then

        Dim colCount As Integer = 0
        Dim weekCounterStart As Integer = 1
        Dim increment As Integer = 3
        Dim maxWeekCounter As Integer = 52
        Dim weekCounter As Integer



        For weekCounter = weekCounterStart To maxWeekCounter Step increment

            weekCounter = weekCounter + increment
            Console.Write(vbTab & "Week " & weekCounter & Environment.NewLine)
            Exit For

            'If it is three columns, write a new line.
            If (colCount >= 2) Then

                colCount = 0
                Console.WriteLine()

            Else
                colCount = +1
            End If


        Next weekCounter

1 个答案:

答案 0 :(得分:1)

为什么在循环体的第一行增加计数器?这已经由循环本身完成了。所以只需删除它:

For weekCounter = weekCounterStart To maxWeekCounter Step increment
    ' weekCounter = weekCounter + increment 'REMOVE!!!
    Console.Write(vbTab & "Week " & weekCounter & Environment.NewLine)
    ' Exit For  'REMOVE!!!

您也会立即退出循环,因此请删除Exit For