java中,用递归函数实现1到N的累加
的有关信息介绍如下:import java.util.Scanner;
public class TestDiGui
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int input = 0;
int show = 0;
System.out.println("输入一个整数n:");
input=sc.nextInt();
show = f(input);
System.out.println("结果为:"+show);
}
static int f(int n)
{
int result = 0;
if(n==0)
{
result = 0;
return result;
}
if(n==1)
{
result = 1;
return result;
}
else
{
result = n+f(n-1);
return result;
}
}
}
public class Test {
long f(int n){
if(n ==1){
return 1;
}
return n+f(n-1) ;
}
public static void main(String args[]){
System.out.print(new Test().f(1000));// N
}
}