public class Test { public static void main(String[] args) { Test a = new Test(); System.out.println(a.getSum(100)); System.out.println(a.getSum1(100)); System.out.println(a.getMul(5)); System.out.println(a.f(10)); } public int getSum(int num) { int sum = 0; for (int i = 0; i <= num; i++) { sum = sum + i; } return sum; } public int getSum1(int num) { if(num == 1) { return 1; } else { return getSum1(num-1) + num; } } public int getMul(int n) { if(n == 1) { return 1; } else { return n * getMul(n - 1); } } public int f(int n) { if(n == 1) { return 1; } else if(n == 2) { return 1; } else { return f(n - 1) + f(n - 2); } } }
public class Test
{
public static void main(String[] args)
{
Test a = new Test();
System.out.println(a.getSum(100));
System.out.println(a.getSum1(100));
System.out.println(a.getMul(5));
System.out.println(a.f(10));
}
public int getSum(int num)
{
int sum = 0;
for (int i = 0; i <= num; i++)
{
sum = sum + i;
}
return sum;
}
public int getSum1(int num)
{
if(num == 1)
{
return 1;
}
else
{
return getSum1(num-1) + num;
}
}
public int getMul(int n)
{
if(n == 1)
{
return 1;
}
else
{
return n * getMul(n - 1);
}
}
public int f(int n)
{
if(n == 1)
{
return 1;
}
else if(n == 2)
{
return 1;
}
else
{
return f(n - 1) + f(n - 2);
}
}
}
JAVA经典之递归测试-----JAVA
于 2023-04-16 13:50:01 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)