通过代码追踪
Tracing through code
我对跟踪代码时得到的结果有疑问。在输出中,其中一个答案是 75,现在我明白了所有其他答案是如何得出的。我知道 int f = quip(b,10);'
调用下面称为 quip 的方法。在那种方法中,您会看到 int x = foo(a,w);
,这就是我迷路的地方。任何人都可以向我解释一下吗?
-谢谢
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
int f = quip(b, 10); // b = 5
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
此处调用方法 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
,因为 w
和 a
正在转换为字符串,因此编译器正在执行字符串连接操作。
x
的值是 25
所以 25*3
是 75
我对跟踪代码时得到的结果有疑问。在输出中,其中一个答案是 75,现在我明白了所有其他答案是如何得出的。我知道 int f = quip(b,10);'
调用下面称为 quip 的方法。在那种方法中,您会看到 int x = foo(a,w);
,这就是我迷路的地方。任何人都可以向我解释一下吗?
-谢谢
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
int f = quip(b, 10); // b = 5
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
此处调用方法 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
,因为 w
和 a
正在转换为字符串,因此编译器正在执行字符串连接操作。
x
的值是 25
所以 25*3
是 75