我不明白为什么SmallCountloopCount的值在提供的代码中从0变为1。我希望它保持在0

问题描述 投票:0回答:2

第一个打印0和第二个打印1。我需要更改什么才能使第二个打印0?

我尝试使用
System.out.println("SMALL LOOP COUNT = " + smallCountLoopCount);

括号来尝试确保数学正确流动,然后先执行乘法,然后再进行第二次。 看起来加法器正在增加变量而不是使用数学?
()

预期结果:

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

。显然,它没有达到任何回报。

如果您更改为
smallCountLoopCount++;
java while-loop
2个回答
1
投票
,那么您将获得所需的输出。

您的循环底部有:

bigCount=0

该条件不会被任何条件包围,因此将始终执行。很难看到您在没有完整代码的情况下要做什么,但是如果您希望SmallCountloopCount保持在零,请删除以下内容:
smallCountLoopCount++;


1
投票

© www.soinside.com 2019 - 2025. All rights reserved.