有些人可以解释我是如何得到这个答案的:
三次二= 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);
}
}
答案 0 :(得分: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
}
答案 1 :(得分:5)
致电sentence()
sentence(one, two, 3);
为了论证,只需要替换所有变量及其值:
sentence( "two", "three", 3);
然后看一下该函数内部参数的值:
three == "two"
one == "three"
number == 3
然后替换你生成的句子中的参数,你就得到了结果!
此外,您的变量名称实际上并不是不言自明的。你应该重新考虑它们以防止这种误解。
答案 2 :(得分:0)
很难猜到你想要在这里实现什么。当然,您不希望将“字符串”文字解释为实际数值......
更正您的变量值:
String one = "one";
String two = "two";
和
sentence(three, one, 3);
和
public static void sentence(String three, String one, int number) {
String str1 = one + " times " + three + " = " + (number);
}
答案 3 :(得分:0)
你给3作为'句子'方法的参数。
sentence(one, two, 3);
将其更改为
sentence(one, two, number);
答案 4 :(得分:0)
String str1 = one + " times " + three + " = " + (number * 2);
此处one
== two
==“三”
和three
== one
==“两个”
和number
== 3
所以你得到了那个。 你推荐的更好:JAVA methods help