Java项目|1-1/2+1/3-1/4+1/5-...1/n 键盘获取n 结果小数;从键盘上输入n result = a+aa+aaa+aaaa+...n部分相加

【Java项目|1-1/2+1/3-1/4+1/5-...1/n 键盘获取n 结果小数; 从键盘上输入n result = a+aa+aaa+aaaa+...n部分相加】1-1/2+1/3-1/4+1/5-…1/n 键盘获取n 结果小数

public class TestDemo {public static void main(String[] args){double sum1=0.0; double sum2=0.0; double sum=0.0; int i; Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); for(i=1; i<=n; i++){if(i%2==0) {sum1=sum1+(1.0/i); }else {sum2=sum2+(1.0/i); } } sum=sum2-sum1; System.out.println(sum);

int a = 1, 从键盘上输入n result = a+aa+aaa+aaaa+…n部分加
public class TestDemo {public static void main(String[] args){int j=0; int a=1; int sum=0; Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); for(int i=1; i<=n; i++){j=j*10+1; sum+=a*j; } System.out.println(sum); } }

    推荐阅读