Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Java programming calculation 1+3! +5! +...+(2n- 1)! The sum of the first 10 items. The code written by the younger brother is as follows
Java programming calculation 1+3! +5! +...+(2n- 1)! The sum of the first 10 items. The code written by the younger brother is as follows
(1) There is something wrong with i=2*i- 1 of the control loop. If I do this, the first cycle is 1, the second cycle is 3 and the third cycle is 2*3- 1=5.

The fourth cycle i=2*5- 1=9, so 7! No, the next cycle is i=2*9- 1= 17, which is even more wrong.

2 Count me in! There is something wrong with the cycle of, only I = I * J. For example, i=3 and J = J * I =1* 3,3 in the second cycle! It should be 1×2×3.

(3) The factorial value is too large to be saved in int type, and the result can be saved in proportion in long type.

Public static void main(String[] args)

{

Length j= 1, and =1;

for(int I = 3; I<= 19; i+=2)

{

j = j *(I- 1)* I;

sum = sum+j;

system . out . println("+I+"! = "+j); //This sentence can be used to output the factorial in the middle, without comment.

}

system . out . println(" sum = "+sum ");

}

The output is as follows:

3! =6

5! = 120

7! =5040

9! =362880

1 1! =399 16800

13! =6227020800

15! = 1307674368000

17! =355687428096000

19! = 12 1645 100408832000

sum = 122002 10 177860 1647