[英]Head First Java 2nd Edition - Page 118, chapter 5
i'm going through an exercise called be the jvm.我正在做一个叫做 be the jvm 的练习。
However, the output that I am supposed to get is completely different to the way i have worked it out.但是,我应该得到的输出与我计算出来的方式完全不同。
I reach y being 15, and my x is 7.我达到 y 是 15,而我的 x 是 7。
But the output should show 13 15 x = 6;但是输出应该显示 13 15 x = 6;
Here is the code:这是代码:
class Output {
public static void main (String [] args ) {
Output o = new Output();
o.go();
}
void go() {
int y = 7;
for (int x = 1; x < 8; x++) {
y++;
if ( x > 4 ) {
System.out.print(++y + " " );
}
if ( y > 14) {
System.out.println("x = " + x );
break;
} // close if
} // close for
} // close go
} // close class
could someone walk through the code with me, and show me where I am exactly going wrong?有人可以和我一起浏览代码,并告诉我我到底哪里出错了吗?
Thank you for your help!谢谢您的帮助!
Okay,好的,
You start with a new output to call the method go()
.您从一个新的输出开始调用
go()
方法。
When this method runs you have two variable, y=7
and x=1
( x
defined within the for
loop).当此方法运行时,您有两个变量,
y=7
和x=1
(在for
循环中定义的x
)。
Pay attention to the format of the for
loop, it states x
is initialised at 1 and only continues until x < 8
which means when x==7
, STOP.注意
for
循环的格式,它指出x
初始化为 1 并且只持续到x < 8
这意味着当x==7
,停止。 Don't process anything in the loop if x
is EQUAL to 8.如果
x
等于8,则不要在循环中处理任何内容。
Each loop increments x
by one as defined by x++
每个循环将
x
增加 1,如x++
所定义
So, stepping through the loop from the start, where x== 1
and y==7
increment y
by one ( y++
).因此,从头开始逐步执行循环,其中
x== 1
和y==7
y
递增 1 ( y++
)。
now we have x==1
and y==8
现在我们有
x==1
和y==8
we can ignore the next two if
statements as x
is less than 4 and y
less than 14, so back to the start of the for loop, don't forget to increment x
by one.我们可以忽略接下来的两个
if
语句,因为x
小于 4 且y
小于 14,所以回到 for 循环的开始,不要忘记将x
加一。
So for the second loop we have: x==2
y==9
.所以对于第二个循环,我们有:
x==2
y==9
。
Third loop: x==3
y==10
第三个循环:
x==3
y==10
Fourth loop: x==4
y==11
<- x
is now 4, but not GREATER than 4. So on the next loop we need to enter the if(x>4)
loop.第四环:
x==4
y==11
< - x
现在是4,但不大于4,所以在下一个循环中,我们需要输入if(x>4)
循环。
Fifth loop: x==5
y==12
(as y++
immediately) but then we enter the loop for when x>4
now, therefore we ++y
.第五个循环:
x==5
y==12
(立即作为y++
)但是我们现在进入当x>4
时的循环,因此我们++y
。 This is probably where you made a mistake这可能是你犯错的地方
++y
is similar to y++
but increments the value, then evaluates and stores it instead of evaluating the value, then incrementing and storing it. ++y
与y++
类似,但增加值,然后评估并存储它,而不是评估值,然后增加并存储它。
This outputs 13 and now y==13
.这输出 13 ,现在
y==13
。
Sixth loop: Same as the fifth loop (logically, enter the first if statement!) x==6
y==13
第六次循环:同第五次循环(逻辑上,进入第一个if语句!)
x==6
y==13
output 15 and now y==15
, so we can enter the final loop, so we output the value of x
for this loop,输出 15 和现在
y==15
,所以我们可以进入最后的循环,所以我们输出这个循环的x
的值,
which is 6.这是 6。
We then break out and that's the end.然后我们爆发,这就是结束。
So the output, should be 13 15 x=6
.所以输出应该是
13 15 x=6
。 I hope this helps, just be aware of the difference between ++y
and y++
.我希望这会有所帮助,只需注意
++y
和y++
之间的区别。
I've got this:我有这个:
x | y | output
1 | 8 |
2 | 9 |
3 | 10 |
4 | 11 |
5 | 12, 13 | 13
6 | 14, 15 | 15 x = 6
Hope it will help you to find your mistake希望它能帮助你找到你的错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.