- package Test01;
- import java.util.Scanner;
- public class Test1_81 {
- public static void main(String[] args)
- {
- while(true)
- {
- System.out.println("请输入一个整数:");
- Scanner sc = new Scanner(System.in);
- int x = sc.nextInt();
- fun(x);
- }
- }
- public static void fun(int n)
- {
- double sum=0;
- if(n%2==0)
- {
-
- for(int i=1;i<=n/2;i++)
- {
- sum+=(double)1/(i*2);
- }
- System.out.println(String.format("%.8f",sum));
- }
- else {
- for(int i=0;i<(n+1)/2;i++)
- {
- sum+=(double)1/(iaaaaa*2+1);
- }
- System.out.println(String.format("%.8f",sum));
- }
- }
- }
复制代码
这里数据类型为什么得强转成double,不是可以自动提升么
|
|