0

我在这个循环中犯了某种错误,我真的想不通。这是循环:

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]);
    }

基本上,它只是快速打印'(First stepname) [*] 0%'。如果它非常明显,我很抱歉,但我有点菜鸟。:)

PS如果你需要更多我的课,请问我。

4

3 回答 3

4

请更改此:

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

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

唯一的比较错误是存在的。

还要确保 的值current tick or totalticks changes for the loop to break,因为可能会发生它们永远不会达到相等状态并且您的循环由于条件而变为无限的while(true)情况。

于 2013-10-29T05:14:46.040 回答
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--)`
    
于 2013-10-29T05:13:15.273 回答
0

你在这里有一个无限while循环。

if(currentTick == totalTicks)
    break;

您似乎没有更改任何一个currentTick或根本没有更改的值,totalTicks但是当上述两个值相等时,应该会发生breakfrom循环。while由于它们最初不相等,并且您永远不会在 中更改它们while,因此它变成了无限循环。

于 2013-10-29T05:15:58.770 回答