有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java实践它!1.2.3:奇怪

我是学习Java的新手,我会练习它!帮助我理解语言的问题

我被题为“奇怪”的问题1.2.3困住了,在这个问题中,他们希望您根据他们提供的代码键入输出

我的问题是,与输入相比,我不理解输出

 public class Strange {

 public static void main(String[] args) {
    first();
    third();
    second();
    third();
 }

 public static void first() {
    System.out.println("Inside first method.");
}

public static void second() {
    System.out.println("Inside second method.");
    first();
 }

 public static void third() {
    System.out.println("Inside third method.");
    first();
    second();
 }
}

我认为结果是:

Inside first method.
Inside third method.
Inside first method.
Inside second method.
Inside second method.
Inside first method.
Inside third method.
Inside first method.
Inside second method.

但事实是:

Inside first method.
Inside third method.
Inside first method.
Inside second method.
Inside first method.
Inside second method.
Inside first method.
Inside third method.
Inside first method.
Inside second method.
Inside first method.

为什么会这样

非常感谢


共 (6) 个答案

  1. # 1 楼答案

    您应该启动一个调试器并逐步执行该代码,这样您就可以看到它在运行时会执行什么操作

    这里要解释的问题是,它做了它应该做的事情(所以程序输出是我所期望的)

    你可以在你(希望)使用的IDE中找到你的调试器。 你必须在这里添加一个断点first();,然后应该能够“跨入”你想要的每个方法,或者“跨过”System.out.println

  2. # 2 楼答案

    输出没有问题。再检查一遍。输出将

    Inside first method.
    Inside third method.
    Inside first method.
    Inside second method.
    Inside first method.
    Inside second method.
    Inside first method.
    Inside third method.
    Inside first method.
    Inside second method.
    Inside first method.
    

    谢谢

  3. # 3 楼答案

    您可以通过应用一些缩进来理解它:

    first
    third
        first
        second
            first
    second
        first
    third
        first
        second
            first
    

    (内部节点表示外部节点的方法调用)

  4. # 4 楼答案

    您应该通过查看每个方法执行的逐步调用来理解这一点

    first();
        Inside first method.
    third();
        Inside third method.
            first();
                Inside first method.
            second();
                Inside second method.
                    first();
                        Inside first method.
    second();
        Inside second method.
            first();
                Inside first method.
    third();
        Inside third method.
            first();
                Inside first method.
            second();
                Inside second method.
                    first();
                        Inside first method.
    
  5. # 5 楼答案

    public static void first() {
        System.out.println("Inside first method.");
    }
    
    public static void second() {
        System.out.println("Inside second method.");
        first();
    }
    

    second()的每次调用都将连续显示以下行(因为first()紧随其后被调用):

    Inside second method.
    Inside first method.
    

    这也是唯一可以打印“Inside second method.”的方法。因此,您预期的输出是不可能的:

    ...
    Inside second method.
    Inside second method.
    
  6. # 6 楼答案

    secondMethod()调用了firstMethod(),这就是为什么在每个“Inside Second Method”后面都会看到“Inside First Method”。删除secondMethod()中对firstMethod()的调用,您将看到预期的输出