我只使用了整个程序中的一部分,但我百分之百肯定它在于这种方法。
public void draw()
{
int count;
int arrowHeadRight = getIndentationLevel()
+ getBaseLength() / 2;
for (count = 0; count < arrowHeadRight; count++)
{
System.out.print(' ');
}
System.out.println(drawingCharacter);
int baseAtCenter = getBaseLength() / 2 - 1;
int arrowHeadLeft = 1;
for (count = 1; count <= baseAtCenter; count++)
{
for (count = 0; count < arrowHeadRight--; count++)
{
System.out.print(' ');
}
System.out.print(drawingCharacter);
for (count = 0; count < arrowHeadLeft; count++)
{
System.out.print(' ');
}
System.out.print(drawingCharacter);
arrowHeadLeft = arrowHeadLeft + 2;
}
for (count = 0; count < arrowHeadRight--; count++)
{
System.out.print(' ');
}
for (count = 1; count <= getBaseLength(); count++)
{
System.out.print(drawingCharacter);
}
System.out.print("\n");
//Arrow Shaft
for (count = 1; count <= shaftLength; count++)
{
for (count = 0; count < arrowHeadRight; count++)
{
System.out.print(' ');
}
System.out.println(drawingCharacter);
}
}
现在假设for
循环中的所有变量都大于count
变量,那么会出现无限循环?我已经检查了很多次,以至于我的眼睛受伤了,看了一双新鲜的东西会有所帮助。
答案 0 :(得分:0)
这是你的无限循环:
for (count = 1; count <= shaftLength; count++)
{
for (count = 0; count < arrowHeadRight; count++)
{
每次内循环终止时,count
将等于arrowHeadRight
。如果arrowHeadRight <= shaftLength
那么外部循环永远不会终止。
答案 1 :(得分:0)
这是带有问题的代码的一部分:
for (count = 1; count <= shaftLength; count++)
{
for (count = 0; count < arrowHeadRight; count++)
{
System.out.print(' ');
}
System.out.println("drawingCharacter");
}
你正在开始一个索引为count=1
的for循环,然后在它内部开始另一个循环,使索引count
保持为0.在内循环之后,count将等于arrowHeadRight
,而不是进入循环时的情况。
你的整个方法都充满了类似的例子。
解决方案是在使用嵌套for循环时使用不同的索引变量:
for (count = 1; count <= shaftLength; count++)
{
for (int count2 = 0; count2 < arrowHeadRight; count2++)
{
System.out.print(' ');
}
System.out.println(drawingCharacter);
}