Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with looping etc THANKS ALOT IN ADVANCE

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with looping etc THANKS ALOT IN ADVANCE

    import io.*;
    	public class count
    	{	
    	public static void main( String [] args)
    	{
    	double x;
    	x=ConsoleInput.readDouble("Enter x value");
    		for(int n=0;n<=10;n++)
    		{
    		eToTheX(x,n);
    		}
    	}
    	public static void eToTheX(double x, int n)
    	{
    	double e=0, nFactorial = 1;	
    		for(int i=2; i <=n; i++)
    		{
    		nFactorial *= (double)i;
    		}
    	e+=Math.pow(x,(double)n)/nFactorial;
    	System.out.println("real ex  " + Math.exp(x) + "  calculated ex  " + e);
    }
    }

    ok so my e+= isnt exactly working... it's just not adding e onto itself each time it displays 11 outputs like i want it to but they dont stack so for e.g. its just x^5/5! when i need x^0/0!+..+..x^5/5! so i need it to output 0,(0..+.. 1),(0+1+2),(0+1+2) thats first 4 results i need that up until its 8+9+10 etc which would be 11 results

    so basically


    Last edited by moesom; April 15th, 2011 at 07:15 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,967
    My Mood
    Hungover
    Thanks
    133
    Thanked 554 Times in 475 Posts

    Default Re: Help with looping etc THANKS ALOT IN ADVANCE

    You're creating a new e variable each time you call the function, so it's going to start over as zero each time.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with looping etc THANKS ALOT IN ADVANCE

    Quote Originally Posted by KevinWorkman View Post
    You're creating a new e variable each time you call the function, so it's going to start over as zero each time.
    can you please show me how to make it work, i've tried everything =/

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,967
    My Mood
    Hungover
    Thanks
    133
    Thanked 554 Times in 475 Posts

    Default Re: Help with looping etc THANKS ALOT IN ADVANCE

    You could return the value from that function, then increment another variable with it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    moesom (April 15th, 2011)

Similar Threads

  1. Replies: 3
    Last Post: February 22nd, 2011, 07:43 PM
  2. looping and arrays
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 23
    Last Post: January 7th, 2011, 09:16 PM
  3. Help in looping
    By endframe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 28th, 2010, 02:24 PM
  4. looping method
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2010, 08:19 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM