Find out e=1+1/1!+1/2!+1/3!+......+1/n!+ ... The approximate value of Java applet program

Source: Internet
Author: User
Tags add
Program//Find out e=1+1/1!+1/2!+1/3!+......+1/n!+ ... The approximate value, the request error is less than 0.0001import java.applet.*;import java.awt.*;import java.awt.event.*;p ublic class At1_1 extends Applet Implements Actionlistener{  textfield Text1; button button1;  public void init ()  {   text1 = new TextField ("0");   button1 = new Button ("purge");   add (Text1);   Add (Button1);   text1.addactionlistener (this);   button1.addactionlistener (this);  }  public void Start () {} public void Stop () {} public void Destory () {} public void paint (Graphics g) & nbsp {   g.drawstring (Enter number n back in text area, 10,100);   g.drawstring ("Text area displays 1+1/1!+1/2!+1/3!+......+1/n! + ... Approximate value ", 10,120);  } public void actionperformed (ActionEvent e)  {  if (E.getsource () ==text1)   {   double Sum=1,a=1;   int I=1;   int n=0;    try   {   n = IntegEr.valueof (Text1.gettext ()). Intvalue ();    while (i<=n)    {      a = A * (1.0/i);     sum = sum + a;    i=i+1;   }    sum=sum*10000;   sum=math.round (sum);   sum=sum/10000;    text1.settext ("" +sum);      }   catch (NumberFormatException Event)    {   text1.settext ("Please enter numeric characters");   }  }   else if (E.getsource () ==button1)   {   text1.settext ("0");  }  &NBSP;&NBSP}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.