39 【程序 39 分数累加】
题目:编写一个函数,输入 n 为偶数时,调用函数求 1/2+1/4+...+1/n,当输入 n 为奇数时,调用函数 1/1+1/3+...+1/n
package cskaoyan;
public class cskaoyan39 {
@org.junit.Test
public void sum() {
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("请输入一个数字:");
int number = in.nextInt();
double result = 0;
if (odd(number)) {
result = oddNumber(number);
} else {
result = evenNumber(number);
}
System.out.println(result);
in.close();
}
public boolean odd(int number) {
if (number % 2 != 0) {
return true;
} else {
return false;
}
}
public double evenNumber(int number) {
double result = 0;
for (int i = 2; i <= number; i += 2) {
result += (double) 1 / i;
}
return result;
}
public double oddNumber(int number) {
double result = 0;
for (int i = 1; i <= number; i += 2) {
result += (double) 1 / i;
}
return result;
}
}