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);
}
}
}文章来源地址https://www.toymoban.com/news/detail-416140.html
文章来源:https://www.toymoban.com/news/detail-416140.html
到了这里,关于JAVA经典之递归测试-----JAVA入门基础教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!