JAVA if problem

• 07-24-2007, 04:39 AM
toby
JAVA if problem
Hi, I am working on a JAVA if problem. Basically it checks if the number inputted is larger than 0. If it is, it does another IF statement which checks if the number is even or odd and returning 0 or 1 respectively. Either one should then return to the original IF problem but with half the n value. Here is what i done but it does not seem to work and returns errors:

Code:

```static String test(int n) {     if (n >0)     {       if (n%2 == 0)         return 1; n+test(n%2);       else         return 0; n+test(n%2);     }       else         return;   }```
Thanks.
• 07-24-2007, 08:07 PM
cruxblack
Code:

```if (n&#37;2 == 0)         return 1; n+test(n%2);       else         return 0; n+test(n%2);```
I haven't tried ur code, but if im not mistaken, if u write return, wouldn't the program got out of the if before it can even do the n+test?:confused:
And also, i haven't tried it, but 2 statements that are written in one line are considered 2 statements after all right?
Maybe u should use braces, just for precaution

And btw, if u could maybe post the error message so we can figure whats wrong?
Hope this helps
• 07-25-2007, 07:58 PM
Seemster
Exactly what the previous poster said. once it encounters return, it exits the block. your code will not compile since the code after the return is unreachable. you could split into 2 methods like this:

public void someMethod() {
int value = 9;

System.out.println("the value: " + value + " is: " + determineValue(value));
System.out.println("n is now: " + n/2);
}

public int determineEvenOdd(int n) {
int returnValue = 0;

if (n > 2) {
returnValue = (n&#37;2 == 0) ? 1: 0;
}

return returnValue;
}