有人帮我弄清楚COUNTER在这个程序中的功能!!! [英] Someone kindly help me figure out the function of COUNTER in this program!!!
问题描述
加仑,升;
int counter;
counter = 0;
for(加仑= 1;加仑<= 10;加仑++)
{
升=加仑* 3.7;
System.out.println(加仑+加仑等于+升+ 升);
counter ++;
if(counter == 2)
{
System.out.println();
counter = 0;
}
}
引用:我希望有人向我解释它背后的逻辑!!
这可以通过代码检查轻松实现,即精神上模拟代码执行。
启动时计数器
等于0
。
在第一次迭代时(在第一个输出行之后)它会增加,编码1
,因此如果
未输入阻止。
在第二次迭代时(在输出行之后),它再次递增,变为2
。现在输入if
块,打印换行符,计数器
再次变为0。这一遍又一遍地重复。
我修改了行缩进。
它做得不好,但从要求的观点。要求是任意的,因此'逻辑'不会做出决定。
代码将在每个循环中增加加仑。
升将是加仑次数3.7
输出打印
每偶数加仑就会得到一条新线。
输出将是:
1加仑等于3.7升
2加仑等于7.4升
3加仑等于11.1升
4加仑等于14.8升
5加仑等于18.5升
6加仑等于22.2升
7加仑等于25.9升
8加仑等于29.6升
9加仑等于33.3升
10加仑等于37升
double 加仑,升;
int counter; // 没有理由不将此行加入下一个
counter = 0 跨度>; // 但不需要计数器
// 这是for循环:(init; limit; increment)
for (加仑= 1 ;加仑< = 10 ;加仑++)
{
升=加仑* 3. 7 ;
系统。 out .println(加仑+ 加仑等于 +升+ 升);
counter ++;
if (counter == 2 )
{
系统。 out .println();
counter = 0 ;
}
// 除计数器外的替代方案是:
// if(加仑%2 == 0)
// System.out.println();
}
这就是我写的方式:
for ( int counter = 1 ;计数器< = 10 ; counter ++)
{
double 加仑=计数器;
double 升=加仑* 3 。 7 跨度>;
系统。 out .println(加仑+ 加仑等于 +升+ 升);
if (counter% 2 == 0 )
系统。 out .println();
}
所以现在计数器
只能由for
循环。这两者是不可分割的。计数器
的值显式绑定到for
循环。
加仑
和升
变量也仅存在于的范围内
循环,但是值隐含地连接到循环。它们没有定义,也没有为循环定义。
我们可以认为双精度是显式的,因为它们是由循环定义的计数器定义的,但是这会让语义妨碍术语。 / blockquote>
调试器是你的朋友。
对于理解程序如何工作的问题,没有什么比调试器更好的了。调试器让你逐步看到执行,这是非常宝贵的。
调试器也是你的朋友,理解程序为什么不做什么你想要的。
double gallons, liters; int counter; counter =0; for(gallons=1; gallons<=10; gallons++) { liters=gallons*3.7; System.out.println(gallons + " gallons is equal to " + liters + "liters"); counter++; if(counter==2) { System.out.println(); counter=0; } }解决方案Quote:I wanted someone to explain to me the logic behind it!!
That could easily achieved by code inspection, that is mentally simulating the code execution.
At startupcounter
is equal to0
.
At first iteration (after first output line) it gets incremented, beocming1
, hence theif
block is not entered.
At second iteration (after th output line) it get incremented again, becoming2
. Now theif
block is entered, the newline is printed andcounter
becomes 0 again. This repeats over and over.
I fixed the line indent.
It's not well done, but it makes sense from a requirement point of view. Requirements are arbitrary so 'logic' doesn't come in to the decision.
The code will increment gallons in each loop.
liters will be gallons times 3.7
the output is printed
every even number of gallons gets a new line under it.
Output will be:
1 gallons is equal to 3.7 liters 2 gallons is equal to 7.4 liters 3 gallons is equal to 11.1 liters 4 gallons is equal to 14.8 liters 5 gallons is equal to 18.5 liters 6 gallons is equal to 22.2 liters 7 gallons is equal to 25.9 liters 8 gallons is equal to 29.6 liters 9 gallons is equal to 33.3 liters 10 gallons is equal to 37 liters
double gallons, liters; int counter; // there is no reason not to join this line with the next counter =0; // but there is no need for a counter // this is a for loop: (init;limit;increment) for(gallons=1; gallons<=10; gallons++) { liters=gallons*3.7; System.out.println(gallons + " gallons is equal to " + liters + "liters"); counter++; if(counter==2) { System.out.println(); counter=0; } // the alternative excluding the counter is: //if(gallons % 2 == 0) // System.out.println(); }
This is how I would have written it:
for(int counter = 1; counter <= 10; counter ++) { double gallons = counter; double liters = gallons * 3.7; System.out.println(gallons + " gallons is equal to " + liters + "liters"); if(counter % 2 == 0) System.out.println(); }
So nowcounter
is defined only by thefor
loop. The two are inseparable. The value ofcounter
is explicitly tied to thefor
loop.
Thegallons
andliters
variables also only exist in the scope of thefor
loop, but there value is implicitly connected to the loop. They are not defined by, nor do they define thefor
loop.
We could consider the doubles as explicit as they are defined by the counter, which is defined by the loop, but that's letting semantics get in the way of terminology.
The debugger is your friend.
For problems like understanding how a program works, there is nothing like the debugger. The debugger let you see the execution step by step and this is invaluable.
The debugger is your friend too when it comes to understanding why a program don't do what you want.
这篇关于有人帮我弄清楚COUNTER在这个程序中的功能!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!