实现累加,即使用循环嵌套函数即可,此处声明一个static函数来完成这项工作,使用if语句判断当前值是否小于1,小于则停止计算。实现代码如下:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ch04_4.pkg16;
/**
*
* @author
*/
public class Ch04_416 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int n=50;
int f=f1(n);
System.out.println("f="+f);
// TODO code application logic here
}
static int f1(int p)
{
if(p<=1)
return 1;
else
return(p+f1(p-1));
}
}