Results 1 to 3 of 3
Thread: JAVA if problem
- 07-24-2007, 04:39 AM #1
Member
- 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:
Thanks.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; }
- 07-24-2007, 08:07 PM #2
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
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: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
- 07-25-2007, 07:58 PM #3
Member
- 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: 02-05-2008, 03:46 AM -
Problem in java
By saytri in forum New To JavaReplies: 4Last Post: 01-16-2008, 10:09 PM -
Problem in java
By saytri in forum New To JavaReplies: 6Last Post: 01-09-2008, 04:13 PM -
Problem with timer in java
By paul in forum Advanced JavaReplies: 3Last Post: 07-26-2007, 10:18 AM -
java SE 6 problem
By techlance in forum Java AppletsReplies: 1Last Post: 06-28-2007, 10:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks