【问题标题】:What is the value of the variable result after the following code is executed?下面的代码执行后变量result的值是多少?
【发布时间】:2016-01-17 16:56:20
【问题描述】:
int count = 0, result = 0;
while(count <= 10)
{
    if(count % 2 ==0)
    {
        result = result + count;
    }
    else
    {
        result = 0;
    }
    count = count + 2;
}
System.out.println("Result: " + result); 

有人能解释一下为什么答案是 30 吗?

【问题讨论】:

  • 在调试模式下运行,你会得到答案。
  • 你是怎么做到的?我正在使用 Jcreater。
  • 您甚至不需要调试器,只需手动单步调试,您就会看到值是 30。只需几次迭代,您就会知道。
  • 缺少调试器,在count = count + 2 行之后添加System.out.println("count=" + count + ", result=" + result);,您将看到每次迭代的变量如何变化。
  • 它清楚地总结了从 0 到包括 10 的偶数。 if(count % 2 == 0) 条件每次都为真,因为您每次都将 2 添加到已经是偶数.

标签: java loops


【解决方案1】:

count0 开始,每次迭代时都会向其添加数字 2。当count 等于或高于10 时,循环停止。因此count 的值将是:

0, 2, 4, 6, 8, 10

这些都是偶数,所以count % 2 == 0 是真的。

result0 开始,并且在每次迭代时将count 添加到其中。因此,最终的result 将是上述所有数字的总和。和

0 + 2 + 4 + 6 + 8 + 10 = 30

【讨论】:

    【解决方案2】:

    首先,计数是00 % 2 == 0,所以if 块被执行(而不是else 块)。 result + count 的计算结果为 0,因此 result 设置为 0。重复 2 (count + 2) 并将其添加到 result,如 2 % 2 == 0

    对于count 的每个值,count % 2 == 0 的计算结果为true。这是因为您每次都将其递增2,因此它将始终保持均匀。因此,else 块是完全多余的,应该被删除。无论如何,您正在做的是将所有偶数相加到 10(包括在内,因为您使用 &lt;=)。这是打印出来的 2 + 4 + 6 + 8 + 10 = 30。

    旁注:看起来像这样的赋值(其中v 是一个变量,o 是一个运算符,e 是一个表达式)可以被简化:

    v = v o e;
    

    本质上(任何人都可以链接我解释涉及铸造的差异的问题吗?)相当于

    v o= e;
    

    算术运算符。

    【讨论】:

      【解决方案3】:

      我刚刚在你的代码中添加了两行代码,现在代码是不言自明的。

      int count = 0, result = 0;
          while(count <= 10)
          {
              if(count % 2 ==0)
              {
                  result = result + count;
                  System.out.println("count%2 " + count%2 + " and Count is " + count + " and Result is " + result);
              }
              else
              {
                  result = 0;
                  System.out.println("count%2 " + count%2 + " Count is " + count + " and Result is " + result);
              }
              count = count + 2;
          }
          System.out.println("Result: " + result);
      

      并在输出后打印到控制台

      count%2 0 and Count is 0 and Result is 0
      count%2 0 and Count is 2 and Result is 2
      count%2 0 and Count is 4 and Result is 6
      count%2 0 and Count is 6 and Result is 12
      count%2 0 and Count is 8 and Result is 20
      count%2 0 and Count is 10 and Result is 30
      Result: 30
      

      我希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        它只是将result 增加每个偶数从 0 到 10。通过将这些数字相加,您会得到30

        0 到 10 之间的偶数包括:

        0, 2, 4, 6, 8, 10
        

        将它们全部加起来,您会得到:

        0 + 2 + 4 + 8 + 10 = 30
        

        您甚至可以通过跟踪 result证明这一点。见下文。

        int count = 0, result = 0;
        do {
            if (count % 2 == 0) {
                System.out.println("Result went from " + result + " to " + (result += count));
                continue;
            }
            result = 0;
            System.out.println("Result is now " + 0);
        } while ((count += 2) <= 10);
        System.out.println("Result: " + result);
        

        【讨论】:

          猜你喜欢
          • 2015-07-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-30
          相关资源
          最近更新 更多