package other;
public class Test {
public static void main(String[] args) {
Test test = new Test();
int maxNum = 100;//累加终值
System.out.println(test.addNum(0,1,maxNum));
System.out.println(test.add(maxNum));
}
/**
* 递归方式进行1到100累加
* @param num1
* @param num2
* @param maxNum
* @return
*/
public int addNum(int num1,int num2,int maxNum){
int sum = 0;
sum = num1 + num2;
if(num1+2 > maxNum){
return sum;
}else if(num2+2 > maxNum){
return sum + num1 + 2;
}else{
return num1 + num2 + addNum(num1+2,num2+2,maxNum);
}
}
/**
* 循环方式进行累加
* @param maxNum
*/
public int add(int maxNum){
int sum = 0;
for(int i = 0;i <= maxNum;i++){
sum = sum + i;
}
return sum;
}
}