2013-10-29 29 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)的条件可能会发生它们永远不会达到平等状态并且循环无限。