-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSummation.java
More file actions
38 lines (35 loc) · 803 Bytes
/
Summation.java
File metadata and controls
38 lines (35 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.bigjava.tq;
import java.util.Scanner;
/**
* 求和
* @author zgp
*
*/
public class Summation {
public static void main(String[] args){
System.out.print("请输入一个整数:");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
if(n%2==0)
System.out.println("结果:"+even(n));
else
System.out.println("结果:"+odd(n));
}
//奇数
static double odd(int n){
double sum = 0;
for(int i=1;i<n+1;i+=2){
sum += 1.0/i;
}
return sum;
}
//偶数
static double even(int n){
double sum = 0;
for(int i=2;i<n+1;i+=2){
sum += 1.0/i;
}
return sum;
}
}