当前位置: 代码迷 >> Java相关 >> 台劳公式求COS(X),结果错误
  详细解决方案

台劳公式求COS(X),结果错误

热度:505   发布时间:2006-04-09 10:11:00.0
台劳公式求COS(X),结果错误
//题目如下:编写一个application应用程序,利用文本框输入X,然后用下列的台劳公式计算cos(x)的值,
//输出之.台劳公式为:cos(x)=1 - x2/2! + x4/4! - x6/6! + x8/8! - ...

import java.awt.*;
import java.awt.event.*;
public class TaiLao extends Frame implements ActionListener
{ Label promptx,promptResult;
TextField xText;
public TaiLao(String strTitle)
{ super(strTitle);
setLayout(new FlowLayout());
promptx=new Label("请输入一个数x");
xText=new TextField(6);
promptResult=new Label(" ");
add(promptx);
add(xText);
add(promptResult);
xText.addActionListener(this);
pack();
show();
}
public static void main(String[] args)
{ TaiLao frameObj=new TaiLao("请输入X");
frameObj.addWindowFocusListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}

});
}
public void actionPerformed(ActionEvent e)
{
int x,oper=-1;
double deshu=1;
x=Integer.parseInt(xText.getText());
for(int i=1;i<10;i++)
{ deshu+=(double)oper*fenZi(x,2*i)/fenMu(2*i);
oper*=-1;

}
promptResult.setText("cos("+x+")= "+deshu);

}
public static int fenMu(int i)//任意项的分母
{ int j=1,total=1;
for(j=i;j>=1;j--)
{ total*=j;
}
return total;
}
public static int fenZi(int a,int i)//求任意项的分子
{ int total=1;
for(int j=1;j<=i;j++)
{ total*=a;
}
return total;
}
}
//运行结果显然错误了
搜索更多相关的解决方案: COS  结果  劳公式  

----------------解决方案--------------------------------------------------------
那就可能是台劳公式错了
----------------解决方案--------------------------------------------------------
  相关解决方案