简体   繁体   English

无法理解Java中的流控制

[英]Unable to understand the flow control in java

I am able to solve the attached code from a coding website.However,My answer is different from the website's answer.Below posted is the code. 我可以从编码网站上解决附加的代码。但是,我的答案与网站的答案不同。下面是代码。

static boolean b1, b2;
public static void main(String [] args) 
{
    int x = 0;
    if ( !b1 ) /* Line 7 */
    {
        if ( !b2 ) /* Line 9 */
        {
            b1 = true;
            x++;
            if ( 5 > 6 ) 
            {
                x++;
            }
            if ( !b1 ) 
                x = x + 10;
            else if ( b2 = true ) /* Line 19 */
                x = x + 100;
            else if ( b1 | b2 ) /* Line 21 */
                x = x + 1000;
        }
    }
    System.out.println(x);
}
}

My idea: 我的点子:

Initially,b1=false and b2= false,it passes the first two if conditions and sets b1 to true besides incrementing the value of x to >1. 最初,b1 = false和b2 = false,如果条件满足,则将前两个参数传递给b1,并将b1设置为true,除了将x的值增加到> 1。 From there on it starts failing for every condition and prints the >final value to be 1. 从那里开始,对于每种情况它都开始失败,并将> final值打印为1。

Website's Solution: 网站解决方案:

As instance variables, b1 and b2 are initialized to false. 作为实例变量,b1和b2初始化为false。 The if tests 如果测试
on lines 7 and 9 are successful so b1 is set to true and x is 第7行和第9行的代码成功执行,因此b1设置为true且x为
incremented. 递增。 The next if test to succeed is on line 19 (note >that the 成功的下一个if测试在第19行(注意>
code is not testing to see if b2 is true, it is setting b2 to be >true). 代码未测试b2是否为真,而是将b2设置为> true)。
Since line 19 was successful, subsequent else-if's (line 21) >will be 由于第19行成功,因此后续else-if(第21行)>
skipped.The final value of x is 101 x的最终值为101

What I do not understand from the website solution: 我对网站解决方案不了解的内容:

"note that the code is not testing to see if b2 is true, it is setting b2 to be true". “请注意,代码没有测试b2是否为真,而是将b2设置为真”。 How can it set the value of b2 to "true" without testing it's status? 如何在不测试其状态的情况下将b2的值设置为“ true”?

Any Suggestions can be highly helpful! 任何建议都将非常有帮助!

Line 19 is: 第19行是:

else if ( b2 = true )

The coding website is probably trying to teach you about a common programming mistake, ie. 编码网站可能正在尝试教您一个常见的编程错误,即。 using assignment = instead of comparison == . 使用赋值=而不是比较==

The code (as written above) is assigning the value of true to b2 rather than comparing the value of b2 to true . (如上面写)的代码的值分配trueb2而非的值进行比较, b2true The result of the assignment expression is true hence the if will always succeed and its code block will be executed. 赋值表达式的结果为true因此if将始终成功,并将执行其代码块。 This will happen regardless of the value of b2 prior to executing this line of code. 无论执行此代码行之前b2的值如何,都会发生这种情况。

If you use what was probably intended, ie: 如果您使用的可能是预期的,即:

else if ( b2 == true )

then a comparison will take place and the if expression (and the execution of its block) will depend on the value of b2 . 然后将进行比较,并且if表达式(及其块的执行)将取决于b2的值。

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

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