Cumulative score

Title: a write function, the input n is even, the function call request 1/2 + 1/4 + ... + 1 / n, n is an odd number when the input calling the function 1/1 + 1/3 + .. . + 1 / n

Copy the code
Import java.util.Scanner. 1; 
 2 {public class Prog39 
 . 3 public static void main (String [] args) { 
 . 4 System.out.println ( "Please enter a positive integer:"); 
 . 5 = new new Scan Scanner Scanner (the System .in); 
 . 6 = n-scan.nextInt int (); 
 . 7 scan.close (); 
 . 8 SUM (n-); 
 . 9} 
10 // summation function 
. 11 public static void SUM (n-int) { 
12 is Double SUM = 0 ; 
13 is IF (n-% 2 == 0) { 
14 for (int I = 2; I <= n-; = I + 2) { 
15 + SUM = 1.0 / I; 
16} 
. 17 System.out.println ( "you entered is an even number, and the calling function is obtained: "SUM +); 
18 is the else {} 
. 19 for (int I =. 1; I <= n-; = I + 2) { 
20 is SUM + = 1.0 / I; 
21 is}
22 System.out.println ( "you entered is an odd number, and the calling function is obtained:" SUM +); 
23 is} 
24} 
25} 
26 is / * operation results 
27 enter a positive integer: 
28. 7 
29 Thou input is an odd number, and call the function is obtained: 1.676190476190476 
30 * /
Copy the code

 

The only way to do great work is to love what you do.

Title: a write function, the input n is even, the function call request 1/2 + 1/4 + ... + 1 / n, n is an odd number when the input calling the function 1/1 + 1/3 + .. . + 1 / n

Copy the code
Import java.util.Scanner. 1; 
 2 {public class Prog39 
 . 3 public static void main (String [] args) { 
 . 4 System.out.println ( "Please enter a positive integer:"); 
 . 5 = new new Scan Scanner Scanner (the System .in); 
 . 6 = n-scan.nextInt int (); 
 . 7 scan.close (); 
 . 8 SUM (n-); 
 . 9} 
10 // summation function 
. 11 public static void SUM (n-int) { 
12 is Double SUM = 0 ; 
13 is IF (n-% 2 == 0) { 
14 for (int I = 2; I <= n-; = I + 2) { 
15 + SUM = 1.0 / I; 
16} 
. 17 System.out.println ( "you entered is an even number, and the calling function is obtained: "SUM +); 
18 is the else {} 
. 19 for (int I =. 1; I <= n-; = I + 2) { 
20 is SUM + = 1.0 / I; 
21 is} 
22 is System.out.println ( "you entered is an odd number, and the calling function is obtained:" SUM +); 
23 is} 
24} 
25} 
26 is / * operation results 
27 enter a positive integer: 
28 7 
29 Thou input is an odd number, and call the function is obtained: 1.676190476190476 
30 * /
Copy the code

 

Guess you like

Origin www.cnblogs.com/exmyth/p/12051982.html
Recommended