Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 05:39 AM
Member
 
Join Date: Jul 2007
Posts: 40
toby is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-24-2007, 09:07 PM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Code:
if (n%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?
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-25-2007, 08:58 PM
Member
 
Join Date: Jul 2007
Posts: 55
Seemster is on a distinguished road
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;
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem to ToolTip in Java 3d roshithmca AWT / Swing 1 02-05-2008 04:46 AM
Problem in java saytri New To Java 4 01-16-2008 11:09 PM
Problem in java saytri New To Java 6 01-09-2008 05:13 PM
Problem with timer in java paul Advanced Java 3 07-26-2007 11:18 AM
java SE 6 problem techlance Java Applets 1 06-28-2007 11:10 AM


All times are GMT +3. The time now is 09:39 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org