Recursive call of exercise method
•
Java
Five people sat together and asked how old the fifth person was? He said he was 2 years older than the fourth person, the fourth said he was 2 years older than the third, the third said he was 2 years older than the second, and the second said he was 2 years older than the first. The first person is 10 years old.
public class Demo35 { public static void main(String[] args) { System.out.println(getAge(5)); } public static int getAge(int n) { if(n==1) return 10; return getAge(n-1)+2; } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码