用java编写程序,计算数学e的值,e=1/1-1/2+1/3-1/4+...+/- 1/n ...怎么编写啊
关注:237 答案:2 手机版
解决时间 2021-01-19 02:35
提问者终究是陌生了
2021-01-18 11:36
import java.util.Scanner;
public class S1014060163 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("请输入一整数(0结束): ");
int n = scan.nextInt();
if (n==0) break;
String 题目字串= myseries(n);
double 结果= myseries_ans(n);
System.out.printf("1...1/%s=%.5f\n",? , ?);
}
}
private static String myseries(int n) {
String res="";
double s=1;
for (int i = 2; i <=n; i++) {
}
return res;
}
private static double myseries_ans(int n) {
double res = 1;
// for 回圈
return res;
}
}
最佳答案
二级知识专家落日海湾
2021-01-18 12:37
import java.util.Scanner;
public class S1014060163 {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
int value = 1;
while (value > 0) {
System.out.println("请输入一整数(0结束): ");
value = scanner.nextInt();
double result = 1.0;
final StringBuilder sb = new StringBuilder("1/1");
for (int index = 2, sign = -1; index <= value; index++, sign *= -1) {
sb.append(sign > 0 ? '+' : '-').append("1/").append(index);
result += (1.0 / (index * sign));
}
if (value != 0) {
sb.append("=").append(result);
System.out.println(sb.toString());
}
}
}
}
应该差不多了,只是不要有非法数据
全部回答
1楼淡似春风
2021-01-18 13:14
System.out.printf("1/1-1/2+..1/%s=%.5f\n", n, 结果);
.............
private static double myseries_ans(int n) {
double res = 1;
boolean sign=true;
for (int i = 2; i<=n; i++) {
if(sign){
res=res-1D/i;
}else{
res=res+1D/i;
}
sign=!sign;
}
return res;
}
我要举报
如以上信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
推荐资讯
大家都在看