package com.abc.test; public class SumTest { public static void main(String[] args) { //题目A:2+4+6+8+.........+40的和,求java算法 sunInteger(); //题目B:1/2+1/4+1/6+1/8+.........+1/40的和,求java算法 sunDouble01(); sunDouble02(); } public static void sunInteger(){ int sum=0; for (int i = 2; i <=40; i+=2) { sum+=i; } System.out.println("sum:"+sum); } public static void sunDouble01(){ int sum=0; for (int i = 2; i <=40; i+=2) { sum+=1/i; } System.out.println("sum:"+sum); } public static void sunDouble02(){ double sum=0D; for (double i = 2; i <=40; i+=2) { sum+=1/i; } System.out.println("sum:"+sum); } }