Click here to Skip to main content
15,936,119 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please check this code snippet.

I made it recursive but it doesn't work. It enters the method again but it doesn't execute the statement in between.
C#
int check_month(int a)
{
    if(a>12)
    {
        a-=12;y1++;
        if(a>12)
        {
            //y1++;
            System.out.println("check");
            check_month(a);
        }
    }
    System.out.println("exit");
    return a;
}


Please check and let me know the error.



Is This A Good Question/Topic? 0

[Edit]
Response from OP - moved here from an answer, since deleted. - Henry

check the code with following calculation
initially a=50
enters method, a>12=true
enters if block. a=a-12;ie 50-12=38. a=38
again enters if 2 . a>12 ie 38>12. recursive procedure called. goes to start.
again a=38
a>12, a=a-12, ie 38-12=26, a=26;
again enters if 2 . a>12 ie 26>12. recursive procedure called. goes to start.
again a=26
a>12, a=a-12, ie 26-12=14 a=14;
again enters if 2 . a>12 ie 14>12. recursive procedure called. goes to start.
again a=14
a>12, a=a-12, ie 14-12=2, a=2;
return 2;
it should work like this , but it doesn't.....!!!

[/Edit]
Posted
Updated 2-Mar-11 5:04am
v4

It is difficult to say what the code needs to do, but line 10 should probably be return check_month(a); The test at line 6 looks wrong too, it is probably redundant.
 
Share this answer
 
One thing I notice is your recursive call in the if statement does not set its return value to anything. Its been a while since I've done any java, but I believe the passed parameter "int a" is its own instance with each call to the function. So the "a" parameter in the first call to this function is not the same variable as the "a" in the next call to this function.

Try changing your recursive line in the if statement to:

a = check_month(a);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900