gpt4 book ai didi

java - 如何a=3和b=4?

转载 作者:行者123 更新时间:2023-12-01 16:33:22 25 4
gpt4 key购买 nike

我发现了一个有趣的编程问题:

执行该程序后a、b、c、f的值是多少?

int i=0,a=0,b=0,c=0,f=0;
while(i<=5){
switch(i++){
case 1:++a;
case 2:++b;
break;
case 3:
case 4:++c;a++;b++;
break;
default:++f;
}
}

我认为值(value)观

a=2、b=2、c=2 和 f=2,但是

当我执行这个程序时,我得到 a = 3、b = 4、c = 2 和 f = 2。

我明白 c 和 f 是如何得到它的值 2 但为什么 a=3 和 b=4。

(根据语法++a 和 a++ 是不同的,因为++a 更新值然后使用它其中 as a++ 使用该值然后更新它)

谁能解释一下 a 和 b 是如何得到 3 和 4 的值的。

更新:

Hey my doubt is : In i++ the intial value is 0 and not 1. But then howcase 4 => a=3

It should be a=2 and should incriment the value ifthere was any updation of 'a' in case 5 (which is not true)as i haven't given anysubstitution like a=a++.

感谢任何帮助。

最佳答案

我建议你用纸和笔来做这个练习。无论如何:

  1. i = 0 ==> f =1;
  2. i = 1 ==> a = 1; b = 1; (情况 1 之后没有中断!)
  3. i = 2 ==> b = 2;
  4. i = 3 ==> c = 1; a = 2; b = 3;
  5. i = 4 ==> c = 2; a = 3; b = 4;
  6. i = 5 ==> f = 2;

关于java - 如何a=3和b=4?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12055319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com