【发布时间】:2016-09-18 14:54:00
【问题描述】:
我的代码的问题是,当我输入 0 时,结果为 0.0,但每当我输入任何大于零的值时,(如 1、2、3、4、5、6、998...任何值)结果在每种情况下始终为 1.0。我的逻辑在迭代中不正确吗?
我的代码:
/*/ Write a y program that will calculate the value of y if the expression
of y is as follows (n is the input):
y = 1/1 + 2/3 + 3/5 + 4/7 + ....... + n/(2n-1) /*/
import static java.lang.System.*;
import java.util.*;
class Practice_Problems04_JavaPrograms_Task04{
public static void main(String[] args){
Scanner orcho = new Scanner(in);
out.println("Please enter the value of n: ");
int n = orcho.nextInt();
double y = 0;
for(int count = 1; count <= n; count++){
y += (count / ((2 * count) - 1));
}
out.println("The summation is, y = " + y);
orcho.close();
}
}
【问题讨论】: