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); } } }