跟踪代码

时间:2015-08-05 04:05:04

标签: java tracing

我对跟踪代码时得到的结果有疑问。在输出中,答案之一是75,现在我理解所有其他答案是如何形成的。我知道int f = quip(b,10);'调用下面称为quip的方法。在那种方法中,你会看到int x = foo(a,w);,这就是我迷路的地方。有人可以向我解释一下吗? -Thanks

public class tracing
{
 public static void main(String[] args)
 {
 int a = 4, b = 5, c = 23;

 int e = foo(c, a);
 int f = quip(b, 10);

 System.out.println(e + ", " + f);
 System.out.println(a + ", " + b);
}
 public static int foo(int x, int y)
 {
 y = x + y;

 System.out.println(x + ", " + y);

 return (x + y);
 }
 public static int quip(int w, int a)
 {
 int x = foo(a, w);

 System.out.println("quip: " + w + a);

 return x * 3;
 }
} 

输出:

23, 27
10, 15
quip: 510
50, 75
4, 5

2 个答案:

答案 0 :(得分:1)

public static int quip(int w, int a) // w = 5, a = 10
{
    int x = foo(a, w);
public static int foo(int x, int y) // x = 10, y = 5
{
    y = x + y; // 10 + 5 = 15, after this line y = 15, x = 10

    System.out.println(x + ", " + y); // prints 10, 15

    return (x + y); // return 25
}
foo()

返回 int x = foo(a, w); // after this line x = 25, w = 5 System.out.println("quip: " + w + a); // prints quip: 510 return x * 3; // returns 25 * 3 = 75 }

main()

返回int f = quip(b, 10); // after this line f = 75, e = 50 System.out.println(e + ", " + f); // prints 50, 75

props.setProperty("mail.host", mailhost);

答案 1 :(得分:0)

此处调用int f = quip(b, 10);值为

的方法
b=5 so w=5 and a=10

方法:

public static int quip(int w, int a)
 {
 int x = foo(a, w);

 System.out.println("quip: " + w + a);

 return x * 3;
 }

System.out.println("quip: " + w + a);

输出为510,因为wa正在转换为字符串,因此编译器正在执行字符串连接操作。

x的值为25,因此25*375