2016-11-28 38 views
0

我的函數遍歷多維數組並且必須滿足條件。該數組包含1和0。對於那些只能連續打印至少4個,最多9個,即允許1111或111111但不允許11或11111111111.在這些之間必須有至少12個零,例如1111000000000000111111。同樣在7套中,連續36個0。後者是我添加的約束,似乎沒有停止運行。這是一個花名冊工具,所以工作日= 7,maxconsecutivehours = 36由於我將這個函數添加到我的代碼中,它並沒有停止運行。 [VBA]

Dim i, j, m, counter1, counter2, counter3, counterA, remainder As Double 

For i = 1 To UBound(posSolution, 1) 
    counter1 = 0 
    counter2 = 0 
     For j = 1 To UBound(posSolution, 2) 
      If posSolution(i, j) = 0 Then 
       Do Until posSolution(i, j) = 1 
        counter1 = counter1 + 1 
       Loop 
        counter2 = counter2 + 1 
         If counter1 >= maxConsecutiveHours Then 
          j = j + 24 * (workDays - counter2) 
          counter1 = 0 
          counter2 = 0 
         Else 
          remainder = maxConsecutiveHours - counter1 
          counter3 = j 
           Do Until posSolution(i, counter3) = 1 
            counter3 = counter3 - 1 
           Loop 
            Do While posSolution(i, counter3) = 1 
             counter3 = counter3 - 1 
             counterA = counterA + 1 
            Loop 
             If (counterA - remainder) >= minWorkHours Then 
              For m = (counter3 + counterA - remainder) To counter3 + counterA 
               posSolution(i, m) = 0 
              Next m 
              j = m + 24 * (workDays - counter2) 
              counter1 = 0 
              counter2 = 0 
              counter3 = 0 
              counterA = 0 
             End If 
         End If 
      End If 
     Next j 
Next i 

我感興趣的是理解爲什麼,因爲我加入此功能的代碼仍然在運行。

回答

1

這個代碼特定位永遠不會退出:

Do Until posSolution(i, j) = 1 
    counter1 = counter1 + 1 
Loop 

posSolution(i, j)的值永遠不會在循環內改變,因此,如果它不等於1,則反覆遞增counter1直至溢出。你有它隱含聲明爲Variant這裏:

Dim i, j, m, counter1, counter2, counter3, counterA, remainder As Double 

只有remainder是明確一個Double - 一切是一個Variant。如果你把多個聲明在同一行,你需要爲逐一指定類型:

Dim i As Double, j As Double, m As Double, etc... 

因爲counter1是一個Variant,它會反覆得到提升,直到它一個Double,並且在溢出之前將耗盡精度。

相關問題