Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java a=3和b=4是如何变化的?_Java_Switch Statement_Post Increment_Pre Increment - Fatal编程技术网

Java a=3和b=4是如何变化的?

Java a=3和b=4是如何变化的?,java,switch-statement,post-increment,pre-increment,Java,Switch Statement,Post Increment,Pre Increment,我发现了一个有趣的编程问题: 执行此程序后,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; } } inti=0,a=0,b=0,c=0,f=0; 而(i a=3) 它应该是a=2,如果 案例5中的

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

执行此程序后,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;
}
}
inti=0,a=0,b=0,c=0,f=0;
而(i a=3)

它应该是a=2,如果 案例5中的“a”有任何更新(这不是真的),因为我没有给出任何更新 像a=a++这样的替换


感谢您的帮助。

请记住,switch语句支持“fall-through”-对于i=2,只有b递增,但是对于i=1,两者都递增。

请记住switch语句支持“fall-through”-对于i=2,只有b是递增的,但是对于i=1,两者都是递增的。

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

  • i=0==>f=1
  • i=1==>a=1;b=1;(案例1后没有中断!)
  • i=2==>b=2
  • i=3==>c=1;a=2;b=3
  • i=4==>c=2;a=3;b=4
  • i=5==>f=2

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

  • i=0==>f=1
  • i=1==>a=1;b=1;(案例1后没有中断!)
  • i=2==>b=2
  • i=3==>c=1;a=2;b=3
  • i=4==>c=2;a=3;b=4
  • i=5==>f=2

  • 前一个答案中最好地解释了前后增量(++a和a++)

    更新 正如在Bayle的注释中指出的,在这些概念的C++和java实现中有一个根本的区别。java是一个简单的例子。
            x = 1;
            y = ++x;  Here y = 2 and x =2 because we first increment x and assign it to y
    
            x = 1;
            y = x++;  But here y = 1 and x = 2 because we first assign x to y and then increment x
    
    in essense , y = x++ is equivalent to those 2 statements
                y =x;
                x = x + 1;
    

    前一个答案中最好地解释了前后增量(++a和a++)

    更新 正如在Bayle的注释中指出的,在这些概念的C++和java实现中有一个根本的区别。java是一个简单的例子。
            x = 1;
            y = ++x;  Here y = 2 and x =2 because we first increment x and assign it to y
    
            x = 1;
            y = x++;  But here y = 1 and x = 2 because we first assign x to y and then increment x
    
    in essense , y = x++ is equivalent to those 2 statements
                y =x;
                x = x + 1;
    

    希望这能帮助你

    When i is 0 
        None of the case matched and went to default
        so a=0,b=0,c=0,f=1;
    
    When i is 1 
        Case 1 and 2 will execute as there is no break after 1;
        so a=1,b=1,c=0,f=1;
    
    When i is 2 
        Case 2 will execute
        so a=1,b=2,c=0,f=1; 
    
    When i is 3 
        Case 3 and 4 will execute as there is no break after 3;
        so a=2,b=3,c=1,f=1; 
    
    When i is 4 
        Case 4 will execute
        so a=3,b=4,c=2,f=1; 
    
    When i is 5 
        Default will execute
        so a=3,b=4,c=2,f=2;
    

    希望这能帮助你

    When i is 0 
        None of the case matched and went to default
        so a=0,b=0,c=0,f=1;
    
    When i is 1 
        Case 1 and 2 will execute as there is no break after 1;
        so a=1,b=1,c=0,f=1;
    
    When i is 2 
        Case 2 will execute
        so a=1,b=2,c=0,f=1; 
    
    When i is 3 
        Case 3 and 4 will execute as there is no break after 3;
        so a=2,b=3,c=1,f=1; 
    
    When i is 4 
        Case 4 will execute
        so a=3,b=4,c=2,f=1; 
    
    When i is 5 
        Default will execute
        so a=3,b=4,c=2,f=2;
    


    我会在调试器中逐步检查你的代码,看看每一行都做了什么。它会显示每一行上每个变量的值。看起来像是meI以前的“突破”作业,希望不是他的作业,或者至少只是额外的学分@edze。有更多有价值的东西可以教给你,比如CS理论,那么switch语句有多糟糕。@PeterLawrey的想法是正确的。在Eclipse和所有其他主流IDE中都有一个易于使用的调试器。单步执行代码总是很有启发性的。当i为1,3和4时,a会递增;当i为1,2,3,4时,b会递增……我会在调试器中单步执行您的代码,以查看每一行的功能。它会在ea处显示每个变量的值ch line。看起来像是老的“突破”作业,meI希望这不是他的作业,或者至少只是额外的学分@edze。有更多有价值的东西可以教,比如CS理论,那么switch语句有多糟糕。@PeterLawrey有正确的想法。Eclipse和所有其他主流IDE中都有一个易于使用的调试器。Stepping thr当我是1,3和4时,A会递增;当我是1,2,3,4时,B会递增;……这是C++。这些运算符在java中有一个更明确的评价。@ fGB在概念的语言实现中有什么根本的区别吗?@ MIMIEAM是的,例如,从你的链接中“编译器可以自由地……”在java中,不是延迟的表达式,而是java中的LValuy。也就是说,C++中没有LValy的概念。这些操作符在java中有一个更明确的评估。@ FGB在概念的语言实现中有什么根本的区别吗?@例如,从你的链接中得到。“编译器可以[…]延迟增量直到表达式结束"-在Java中不是这样的。Java中也没有左值的概念。这意味着在这种情况下,i++和++i之间的差异不是问题。wright!@R41nB0wM47r1z在这种情况下不是这样的,但是如果你在哪里赋值,比如a=++x或a=x++;如果开关包含++i,那么第一次迭代将从f开始rom 1,而不是从0开始。这意味着f的值最终将是1而不是2。嘿,我的疑问是:在i++中,初始值是0而不是1。但是,情况4=>a=3(它应该是a=2,如果在情况5中有任何“a”的更新,则应该包含该值,因为我没有给出任何类似a=a++)的替换)哼,没有人回答这个疑问!在您的ans案例5=>a=3中(如果案例5中有任何“a”的更新,那么它应该是a=2,并且应该包含该值,因为我没有给出任何类似于a=a++的替换)这意味着在这种情况下,i++和++i之间的差异不是问题。wright!@R41nB0wM47r1z在这种情况下不是正确的,但是如果你在其中赋值,比如a=++x或a=x++;如果开关包含++i,那么第一次迭代将从1开始,而不是从0开始。这意味着最后f的值将是1和否t 2.嘿,我的疑问是:在i++中,初始值是0,而不是1。但是,情况4=>a=3(它应该是a=2,如果在情况5中有“a”的任何更新,应该包含该值,因为我没有给出任何类似于a=a++)的替换。哼,没有人回答这个疑问!在您的ans情况5=>a=3(它应该是a=2,如果在案例5中有任何“a”的更新,则应该包含该值,因为我没有给出任何类似于a=a++的替换)