package com.company; //计算1+1/2+1/3+1/4....前20项 public class Main { public static void main(String arg[]) { double sum = 0, item = 1; int i = 1, n = 20; while (i <= n) { sum += item; i++; item = 1.0 / i; } System.out.println("sum=" + sum); } }
package com.company; //计算1+1/2+1/3+1/4....前20项 public class Main { public static void main(String arg[]) { double sum = 0, item = 1; int i = 1, n = 20; while (i <= n) { sum += item; i++; item = 1.0 / i; } System.out.println("sum=" + sum); } }