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 04-15-2008, 03:14 PM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
what is wrong with this code
Hello i have an equality that take the following form:
y = 1-1/2+1/3-......................+1/x
x is avariable entered by the user
I try the following Code but it dosn't Work
Code:
int i,x; double y=1.0 ; System.out.print("Please Enter A Number "); x = console.nextInt(); for( i = 2;i <= x;i++) { if((i % 2) == 0) y = y + (1 / i); else y = y - (1 / i); i++; } System.out.print("The Result OF Y is "+ y ); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-15-2008, 03:54 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road

Please give some more details.

+1 and -1 are fix.

in this case x = 6?

y = 1-1/2+1/3-1/4+1/5-1/6+1

If i am wrong then please correct me.


sanjeev
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-15-2008, 06:25 PM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
First of all i had to add the code to get the input from the user, not sure if you have this back somewhere in your program, but for me "console" was undefined. Just a few "standard" things, define i in your for loop and dont bother incrementing it in your loop. The for loop does that for you(no pun intended). Also, throw brackets into your if statement. It saves lines leaving them out, but if you ever have to add something in the future you'll forget them and have all kinds of errors, better to just get in the practice of putting them in but that's your preference. The root problem you are having is that your "i" is an integer and is doing integer division. Which 1/x where x>1 is going to end up as a decimal which in integer division will be a zero. We counteract this by thrown a "(double)" in front of it to cast it as a double and force it to do float division. The code below worked for me and should work for you.

Side thought: you mentioned that your equation is y = 1-1/2+1/3-......................+1/x. If that is so then i think your if statement may be backwards. Make it a "!=" to have the even fractions subtracted instead of the odd ones. Right now it is vise verse.
Code:
int x; double y = 1.0; Scanner console = new Scanner(System.in); System.out.print("Please Enter A Number "); x = console.nextInt(); for (int i = 2; i <= x; i++) { if ((i % 2) == 0) { y = y + (1 / (double)i); } else { y = y - (1 / (double)i); } } System.out.print("The Result Of Y is " + y);

Last edited by Chris.Brown.SPE : 04-15-2008 at 06:29 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-15-2008, 09:17 PM
Member
 
Join Date: Mar 2008
Posts: 7
saint_jorjo is on a distinguished road
public static void main(String[] args) {

double result = 1;
int n = 4;
double m=1;
boolean verno=true;
for(int i=2; i<=n; i++){
if(verno==true){
result = result-m/i;
System.out.println(result+"result0");
verno=false;
}else{result = result+m/i;
System.out.println(result+"result1");
verno=true;
}
}
System.out.println(result);
}
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-15-2008, 09:59 PM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
Is that supposed to do something different jorjo?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-16-2008, 09:27 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
Thanks to your efforts with me
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
I am Doing Something Wrong But Don't Know What? BHCluster New To Java 3 04-16-2008 02:16 PM
Image problems, what's wrong here. Bluefox815 Java Applets 1 03-07-2008 03:45 AM
What's wrong with this code? Wizard wusa New To Java 14 01-23-2008 12:55 AM
Is there somethign wrong with this code? Soda New To Java 1 12-08-2007 05:46 PM
Whats wrong with my code??? Soda New To Java 2 12-06-2007 01:54 PM


All times are GMT +3. The time now is 09:38 AM.


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