4 回答
已采纳
干枯的海绵
TA贡献8条经验 获得超3个赞
package com.haimian;
public class Hello {
//main方法;
public static void main(String[] args) {
int num=3;
Hello hello=new Hello();
System.out.println(hello.function(num));
}
public double function(int num){
double i=1; double count=0;
while(i<num){
count=count+1/i;
i=i+1;}
return count;
}}
小树要长成大树
TA贡献1条经验 获得超0个赞
public class WhilePractice {
/**
* 计算1+1/2!+......+1/20!
*/
public static void main(String[] args) {
int i = 1,j=1;
double sum =0;
while (i<=20) {
j = j*i; //做阶乘(1~20)
sum = sum+(1.0)/j; //定义sum为double类型,用1.0除以j
i++;
}
System.out.println(sum);
}
}
qq___524
TA贡献171条经验 获得超74个赞
public class aaa{ public static void main(String args[]){ double ans = 0; long i, sum; i = 1; sum = 1; while(i < 20){ i++; sum *= i; System.out.println(sum); ans += 1.0*i/sum; } System.out.println(ans); } }
如果计算到21!的话,long类型的数会溢出
添加回答
举报
0/150
提交
取消