The problem lies in J's cycle. Now J has been cycled, and the value of J is changed by j=i+ 1, and the result is infinite cycle.
Solution: Introduce new parameters. For example, int k, replace j=i+ 1 with k=i+ 1, so that the cycle can proceed smoothly.