Results 1 to 3 of 3
Thread: JAVA if problem
 07242007, 04:39 AM #1Member
 Join Date
 Jul 2007
 Posts
 40
 Rep Power
 0
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:
Java 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; }
 07242007, 08:07 PM #2Senior Member
 Join Date
 Jul 2007
 Posts
 130
 Rep Power
 0
Java Code:if (n%2 == 0) return 1; n+test(n%2); else return 0; n+test(n%2);
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
 07252007, 07:58 PM #3Member
 Join Date
 Jul 2007
 Posts
 55
 Rep Power
 0
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%2 == 0) ? 1: 0;
}
return returnValue;
}
Similar Threads

Problem to ToolTip in Java 3d
By roshithmca in forum AWT / SwingReplies: 1Last Post: 02052008, 03:46 AM 
Problem in java
By saytri in forum New To JavaReplies: 4Last Post: 01162008, 10:09 PM 
Problem in java
By saytri in forum New To JavaReplies: 6Last Post: 01092008, 04:13 PM 
Problem with timer in java
By paul in forum Advanced JavaReplies: 3Last Post: 07262007, 10:18 AM 
java SE 6 problem
By techlance in forum Java AppletsReplies: 1Last Post: 06282007, 10:10 AM
Bookmarks