Recursive problem of __java in JAVA

Source: Internet
Author: User

int j=0;
private void Xuhao () {for
(int j = 0; J < 5; J +) {
if (zu>0&&zu<7) {
   dosth ();           
} else {
Xuhao ();
break;
}}}


Problem, there are two loops when calling recursion: The recursive loop ends and then returns to the recursive two cycles

The reason J is a local variable, and when recursive, a new j is defined, and the original is still there, resulting in two cycles

Resolution 1: Turning J into a global variable

Resolution 2: Break after recursion

Solution 3:

Using continue

private void Xuhao () {
int j=0;
for (;j<5;) {           
if (zu>0&&zu<7) {
   dosth (); 
    j + +;      
} else {
continue;
}
}
}

Apply recursion so every time it goes wrong, it starts at the first time.

With continue, it starts with the current error position.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.