Java意外输出

Ste*_*veo 0 java

有人可以解释我是如何得到这个答案的:

三次两次= 6次

码:

public class Params1 {
    public static void main(String[] args) {
        String one = "two";
        String two = "three";
        String three = "1";
        int number = 20;
        sentence(one, two, 3);
    }

    public static void sentence(String three, String one, int number) {
        String str1 = one + " times " + three + " = " + (number * 2);
    }

}
Run Code Online (Sandbox Code Playgroud)

Den*_*ret 7

这是一个有用的图表:

在此输入图像描述

我希望它很清楚.

以下是如何使相同的代码更容易混淆:

    public static void main(String[] args) {
        String a = "three";
        String b = "two";
        sentence(a, b, 3);
    }

    public static void sentence(String a, String b, int number) {
        String str1 = a + " times " + b + " = " + (number * 2);
        System.out.println(str1); // to let you inspect the value
    }
Run Code Online (Sandbox Code Playgroud)


Sir*_*rko 5

在打电话给 sentence()

sentence(one, two, 3);
Run Code Online (Sandbox Code Playgroud)

为了论证,只需用它们的值替换所有变量:

sentence( "two", "three", 3);
Run Code Online (Sandbox Code Playgroud)

然后看一下该函数内部参数的值是什么:

three == "two"
one == "three"
number == 3
Run Code Online (Sandbox Code Playgroud)

然后替换你生成的句子中的参数,你就得到了结果!

此外,你的变量名称并不是真的不言自明.你应该重新考虑它们以防止这种误解.