编写方法,求如下数值:1+1/3+ … + … +1/(2n-1); 请问这个用JAVA该怎么做呢
展开全部
class Count {
public double count(int n) {
double sum = 0;
for (int i = 1; i <= n; i++) {
double temp = (double) 1 / (2 * i - 1);
sum = sum + temp;
}
return sum;
}
public static void main(String[] args) {
Count c = new Count();
System.out.println("sum=" + c.count(100));
}
}
public static void main(String[] args){
Count c = new Count();
System.out.println("sum="+c.count(100));
}
}
public double count(int n) {
double sum = 0;
for (int i = 1; i <= n; i++) {
double temp = (double) 1 / (2 * i - 1);
sum = sum + temp;
}
return sum;
}
public static void main(String[] args) {
Count c = new Count();
System.out.println("sum=" + c.count(100));
}
}
public static void main(String[] args){
Count c = new Count();
System.out.println("sum="+c.count(100));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询