"Give the output that would be seen in the Eclipse console window"(需要解释)

"Give the output that would be seen in the Eclipse console window" (Explanation needed)

import java.awt.*;
import javax.swing.*;

 public class S17Exam2Q5 extends JApplet {
      int x = 5, y = 100, z = 15;
      public void init( {
         int x = z;
         char z = 'X';
         x = x - 3;
         System.out.println("1: x, y, and z are "+x+" "+y+" "+z);

         if (x <= y) {
             x = 50;
             y = y - 5;
             int y = x + 3;
             z = 'Q';
             System.out.println("2: x, y, and z are "+x+" "+y+" "+z);
          }

          System.out.println("3: x, y, and z are "+x+" "+y+" "+z);

          procExt(int z);

          System.out.println("4: x, y, and z are "+x+" "+y+" "+z);
     }

     public void procExt(x) {
        z = 10;
        int x = 20;
        y = y + 1; 
        System.out.println("5: x, y, and z are "+x+" "+y+" "+z);
     }
}

根据考试答案,答案应该是:

1: x, y, and z are 12, 100, X
2: x, y, and z are 50, 53, Q
3: x, y, and z are 50, 95, Q
5: x, y, and z are 20, 96, 10
4: x, y, and z are 50, 96, Q

答案只对 1 和 2 有意义,我在 3 时感到困惑。 3 和 2 不应该是一样的吗,因为它们一个接一个地打印,中间什么也没有发生? 5 和 4 不应该一样吗?我对 procExt 子例程发生了什么也有点困惑我写的答案如下(星号表示错误答案)

1: x, y, and z are  12,  100,   X
2: x, y, and z are  50,   53,   Q
3: x, y, and z are  50,  *53*,  Q
5: x, y, and z are  20,  *54*,  10
4: x, y, and z are *20*, *54*, *10*

init 是一种覆盖其内部从 JApplet 开发的所有内容的方法。发生的事情只是覆盖了 if 的范围。当您在 init 上初始化现有变量时,java 将接受它作为在其范围内具有相同名称的新变量。

2 和 3 不同的原因是因为#2 中的 "y" 基于语句 int y = x + 3;,它只存在于其 if 语句的范围内。第 3 行基于语句 y = y - 5; 但它存在于 if 语句的范围之外,因为它是在 if 语句开始之前声明的(代码行 5)。

5和4的区别是相似,范围不同。 #5 使用在 procExt(x) 方法中声明的 x,这是在 #4.

中使用的不同的 x

3: int y(上次使用)为 53。这并没有覆盖 y,它仍然设置为 95。println 使用了 'int' 值,但下次调用 y 时默认为(非整数数值)95,程序仍然为 y 保存。

对于#4 和#5:您是要将问题列为 1,2,3,4,5 吗?两次都是 5 和 4,所以我会按照我看到的顺序引用它们 (1,2,3,5,4)

最后做#5 最有意义,即使我在其他地方列出了它们。

5:x 是使用 int = 20 设置的,所以这次 x 的读数将为 20,y = y + 1 会将 y 推到 96(并保持 4)并且 z = 10,因为它是放在那里。

4:记住,#3 有 x = 50,y = 95,z = Q。注意 proc() 在 4 之前运行。使用 proc(int z) 保留 #3 中的 'Q' 值一次。因为 proc() 被调用,y + 1 值计算一次等于 96。最后 z = 10 因为 (int z) 调用只将 z 的内容保持在值 Q 一次,z 的实际值是 'int' 20 至少一次。这是由于 int 重载了以前的值。该值在使用后弹出。