2013-10-29 85 views
0

我在這個循環中犯了一些錯誤,我真的無法弄清楚。這裏是循環:While循環問題:不會暫停

while (true) { 
     System.out.print(stepName[currentTick]); 

     for(int i = stepName[currentTick].length() - longestNameInt; i == 0; i--) 
      System.out.print(" "); 

     System.out.print(" ["); 

     double percentCalc = (double) stepPercent[currentTick]; 
     int slotsRep = (int) Math.round((percentCalc * 0.2)); 

     for(int i = slotsRep; i == 0; i--) 
      System.out.print("*"); 

     for(int i = 20 - slotsRep; i == 0; i--) 
      System.out.print(" "); 

     System.out.print("] " + stepPercent[currentTick] + "% \r"); 

     if(currentTick == totalTicks) 
      break; 

     Thread.sleep(stepTime[currentTick]); 
    } 

基本上,它只是保持打印'(第一stepname)[*] 0%'迅速。 對不起,如果它是超級明顯的,但我有點菜鳥。 :)

P.S.請問我是否需要更多的課程。

+0

您能否提及當前tick和totalticks的預期值,可能會發生這樣的情況:他們將會破壞並且您的環路變得無限 – Prateek

+0

如果任何解決方案解決您的問題而不是接受SO上的答案。 – Prateek

回答

0

你有一個無限的while循環這裏。

if(currentTick == totalTicks) 
    break; 

你似乎沒有被改變或者currentTicktotalTicks,不過在breakwhile循環的價值應該在上述兩個2個的值相等的情況發生。由於它們最初並不相同,並且您從不在while中更改它們,所以它變成了一個無限循環。

1

製作如下更正

  1. for(int i = stepName[currentTick].length() - longestNameInt; i >= 0; 我 - )

    2. `for(int i = slotsRep; i >= 0; i--) 
    
        3. `for(int i = 20 - slotsRep; i == 0; i--)` 
    
4

請更改此:

for(int i = stepName[currentTick].length() - longestNameInt; i == 0; i--) 

TO

for(int i = stepName[currentTick].length() - longestNameInt; i >= 0; i--) 

只有比較錯誤在那裏。

還要確保current tick or totalticks changes for the loop to break的值,因爲while(true)的條件可能會發生它們永遠不會達到平等狀態並且循環無限。