我广泛研究了我所参加的在线课程,但没有找到答案。 这是我的困惑: 我不明白为什么在提供的代码中
smallCountLoopCount
的值从 0 变为 1。 我预计它会保持在 0。我使用 IntelliJ IDEA 进行测试。 我有两个报表来审计这些值。 每一个都是:
System.out.println("SMALL LOOP COUNT = " + smallCountLoopCount);
第一个打印 0,第二个打印 1。我需要更改什么才能让第二个打印 0?
我尝试使用
()
括号来尝试确保数学流程正确,先进行乘法,然后再进行加法。 看起来加法部分是在增加变量而不是用它做数学??
while (bigCountLoopCount <= bigCount) {
//System.out.println(bigCountLoopCount + " " + smallCountLoopCount);
if ((bigCountLoopCount * 5) == goal) {
//System.out.println("THIS TRUE ACTIVATED");
return true;
}
System.out.println("SMALL LOOP COUNT = " + smallCountLoopCount);
if (((bigCountLoopCount * 5) + smallCountLoopCount) == goal)
{
System.out.println("SMALL LOOP COUNT = " + smallCountLoopCount);
System.out.println("THIS TRUE ACTIVATED by:");
System.out.println(bigCountLoopCount + " " + smallCountLoopCount + " " + goal);
return true;
}
smallCountLoopCount++;
bigCountLoopCount++;
}
预期结果:
SMALL LOOP COUNT = 0
SMALL LOOP COUNT = 0
实际结果:
SMALL LOOP COUNT = 0
SMALL LOOP COUNT = 1
你的 while 循环的底部有:
smallCountLoopCount++;
这没有任何条件包围,因此将始终被执行。如果没有完整的代码,很难看出您到底想做什么,但如果您希望smallCountLoopCount保持为零,请删除上面的内容,如下所示:
//System.out.println(bigCountLoopCount + " " + smallCountLoopCount);
if ((bigCountLoopCount * 5) == goal) {
//System.out.println("THIS TRUE ACTIVATED");
return true;
}
System.out.println("SMALL LOOP COUNT = " + smallCountLoopCount);
if (((bigCountLoopCount * 5) + smallCountLoopCount) == goal)
{
System.out.println("SMALL LOOP COUNT = " + smallCountLoopCount);
System.out.println("THIS TRUE ACTIVATED by:");
System.out.println(bigCountLoopCount + " " + smallCountLoopCount + " " + goal);
return true;
}
// smallCountLoopCount++ was here - Anything in this area will be executed regardless
bigCountLoopCount++;
}
这是因为循环体末尾有
smallCountLoopCount++;
。显然它没有达到任何回报。
如果更改为
goal=0
和 bigCount=0
那么您将获得所需的输出。