你能解释一下这个Java代码的输出吗?
int a=5,i;
i=++a + ++a + a++;
i=a++ + ++a + ++a;
a=++a + ++a + a++;
System.out.println(a);
System.out.println(i);
Run Code Online (Sandbox Code Playgroud)
两种情况下的输出均为20
我目前正在研究Java教程,现在正在使用Recursions.
我有以下代码来计算传递给阶乘方法的任何数字的阶乘
public class App {
public static void main(String[] args) {
//E.g 4! = 4*3*2*1(factorial 4)
System.out.println(factorial(4));
}
private static int factorial(int value){
//System.out.println(value);
if (value == 1){
return 1;
}
return factorial(value - 1)*value;
}
}
Run Code Online (Sandbox Code Playgroud)
我无法理解这部分内容
if (value == 1){
return 1;
}
return factorial(value - 1)*value;
Run Code Online (Sandbox Code Playgroud)
我的理解是return关键字只是终止方法,和/或返回方法声明的相同类型的值(即int,String等).
运行以下行时会发生什么?
return factorial(value - 1)*value;
Run Code Online (Sandbox Code Playgroud)
该函数返回总值(value - 1)*值,这将给我
(3)*4 = 12
(2)*3 = 6
(1)*2 = 2
Run Code Online (Sandbox Code Playgroud)
每次通过迭代.然而,System.out.println(factorial(4));给我一共24.这个数字是如何从这个方法得出的?没有变量来存储值的总和,那么存储它们的程序在哪里?另外,我如何24从这些值中获益?
(3)*4
(2)*3
(1)*2
Run Code Online (Sandbox Code Playgroud)
虽然我知道这24 …