找不到无限循环但它是 运行 一个?
Can't find an infinite loop but it's running with one?
我只使用了整个程序的一部分,但我 100% 确定它就在这个方法中。
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
变量,哪里会出现死循环呢?我已经检查了很多次了,我的眼睛受伤了,再看看这个会有所帮助。
这是你的无限循环:
for (count = 1; count <= shaftLength; count++)
{
for (count = 0; count < arrowHeadRight; count++)
{
每次内循环终止时,count
将等于 arrowHeadRight
。如果 arrowHeadRight <= shaftLength
那么外循环将永远不会终止。
这是有问题的部分代码:
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);
}
我只使用了整个程序的一部分,但我 100% 确定它就在这个方法中。
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
变量,哪里会出现死循环呢?我已经检查了很多次了,我的眼睛受伤了,再看看这个会有所帮助。
这是你的无限循环:
for (count = 1; count <= shaftLength; count++)
{
for (count = 0; count < arrowHeadRight; count++)
{
每次内循环终止时,count
将等于 arrowHeadRight
。如果 arrowHeadRight <= shaftLength
那么外循环将永远不会终止。
这是有问题的部分代码:
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);
}