(1):求1~100的和:
package LianXi;
public class lianxi {
public static void main(String[] args) {
System.out.println(getCount(100));
}
public static int getCount(int n){
if (n == 1){
return 1;
}
return n + getCount(n - 1);
}
}
(2):求第N项斐波那契数列的值?
package LianXi;
public class lianxi {
public static void main(String[] args) {
System.out.println(fiBo(5));
}
//递归求菲波那切数列的值
public static int fiBo(int n){
if (n == 1){ //设置第一项的值
return 1;
}
if (n == 2){ //设置第二项的值
return 2;
}
return fiBo(n - 1) + fiBo(n - 2);
}
//循环求菲波那切数列的值
public static int fiBo(int n ){
int first = 1, second = 2;
int temp = 0;
for (int i =