简体   繁体   English

是我错了还是结果错了?

[英]Am i wrong or the result is wrong?

在此处输入图片说明

I have a java example, I know it's very easy, but I'm just a beginner.我有一个 Java 示例,我知道这很简单,但我只是一个初学者。 I have a problem in line 8 and 9:我在第 8 行和第 9 行有问题:

while (tailFeathers > 1){
System.out.print(--tailFeathers + " ");}

I think it will print "5 2" because 1 is not greater than 1, but the answer is E, which is "5 2 1".我认为它会打印“5 2”,因为 1 不大于 1,但答案是 E,即“5 2 1”。 Could u explain to me?你能给我解释一下吗? Am I wrong or strange result?我错了还是奇怪的结果? Thanks so much.非常感谢。

Follow the logic closely严格遵循逻辑

It seems you agree that that at line 7. The output to the screen is "5 "您似乎同意在第 7 行。屏幕上的输出是"5 "

At this point tailFeathers = 3此时tailFeathers = 3

3>1 so enter the loop, decrement tailFeathers before using it. 3>1所以进入循环,在使用之前减少tailFeathers

So the screen output is now "5 2 "所以屏幕输出现在是"5 2 "

At this point tailFeathers = 2此时tailFeathers = 2

2>1 so enter the loop, decrement tailFeathers before using it. 2>1所以进入循环,在使用之前减少tailFeathers

So the screen output is now "5 2 1"所以屏幕输出现在是"5 2 1"

at this point tailFeathers = 1此时tailFeathers = 1

1=1 don't enter the loop 1=1不进入循环

screen output is "5 2 1"屏幕输出为"5 2 1"

I think it will print "5 2" because 1 is not greater than 1, but the answer is E, which is "5 2 1".我认为它会打印“5 2”,因为 1 不大于 1,但答案是 E,即“5 2 1”。 Could u explain to me?你能给我解释一下吗?

You are correct in thinking that the loop body will not be entered when tailFeathers==1 , but you misunderstand what will be printed.您认为当tailFeathers==1时不会进入循环体是正确的,但是您误解了将打印的内容。

Here's the loop:这是循环:

while (tailFeathers > 1) {
     system.out.print(--tailFeathers + " ");
}

Let's suppose that the loop body was entered because tailFeathers==2 which is > 1 .假设进入循环体是因为tailFeathers==2 > 1 The system.out.print(--tailFeathers + " ") statement will do several things in a well defined order: system.out.print(--tailFeathers + " ")语句将以明确定义的顺序做几件事:

  1. It will decrement the value of tailFeathers (ie, it will set tailFeathers=1 ),它将减少tailFeathers的值(即,它将设置tailFeathers=1 ),
  2. It will call library functions to create a new String , "1" , to express the new, decimal value of tailFeathers ,它将调用库函数来创建一个新的String"1" ,来表示tailFeathers的新十进制值,
  3. It will concatenate the string "1" with the string " " to create a new string, "1 " , and then finally,它会将字符串"1"与字符串" "以创建一个新字符串"1 " ,然后最后,
  4. It will call system.out.print("1 ") .它将调用system.out.print("1 ")

The key concept here is the meaning of --tailFeathers .这里的关键概念是--tailFeathers的含义。 The value of that expression is the value of tailFeathers after the variable has been decremented.该表达式的值是该变量递减tailFeathers的值。

If you changed it to tailFeathers-- , you'd get a different result.如果将其更改为tailFeathers-- ,则会得到不同的结果。 In that case, the value of the expression is the value that tailFeathers had before it was decremented (ie, 2 in my example, above.)在这种情况下,表达式的值是tailFeathers递减之前的值(即,在上面的示例中为2

The way to remember this is to read it from left to right:记住这一点的方法是从左到右阅读:

  • --t means first, decrement t , then use the new value of t , and --t装置第一,递减t ,然后使用的新值t ,和
  • t-- means first, use the original value of t , and then some time later, decrement t . t--表示首先使用t的原始值,然后一段时间后,递减t

--counter => counter = counter-1. --counter => counter = counter-1。 Its value gets updated instantly and this is how the the pre operator works.它的值会立即更新,这就是 pre 运算符的工作方式。 Also, remember there is while loop in the last, so it prints recursively until the condition becomes false.另外,请记住最后一个 while 循环,因此它会递归打印,直到条件变为假为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM