import java.util.Scanner; public class First { /* * 编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n */ public static void main(String[] args){ Scanner b=new Scanner(System.in); System.out.println("请输入一个数:"); int a=b.nextInt(); int i; double sum; if(a%2==0){ for(i=1,sum=0;i<=a;i++) sum +=1.0/(i*2); System.out.println("1/2+1/4+...+1/"+a+"="+sum); } else{ for(i=0,sum=0;2*i+1<=a;i++) sum +=1.0/(i*2+1); System.out.println("1/1+1/3+...+1/"+a+"="+sum); } } }