编写一个Java程序实现级数运算。
package com.kk.language_faculty.exception;
public class TestThrowsException1 {
public static void main(String[] args) {
TestThrowsException1 te = new TestThrowsException1();
te.check(new int[] { 1, 2, -3, 4, 5, 6, 10, -1, });
}
public void check(int[] arr) {
for (int i : arr) {
if (i 0) {
try {
throw new NegativeArraySizeException(arr);
} catch (NegativeArraySizeException ne) {
System.out.println(“发生了异常, 正在处理中…” + ne.toString());
}
} else {
System.out.println(“— ” + i + ” —“);
}
}
}
}
@SuppressWarnings(“serial”)
class NegativeArraySizeException extends Exception {
private int[] array;
public NegativeArraySizeException(int[] array) {
this.array = array;
}
public int[] getArray() {
return array;
}
}
/////////////////////////
package com.kk.language_faculty.exception;
/*
* 创建一个类Example,有一个String 型参数的方法—check() ,该方法用来检查参数中是否包含英文字母以外的字符。
* 如果包含,则抛出一个MyException异常。
*/
public class TestThrowsException2 {
public void check(String str) {
char temp = ‘ ‘;
for (int i = 0; i str.length(); i++) {
temp = str.charAt(i);
if ((temp = ‘a’ temp = ‘z’) || (temp = ‘A’ temp = ‘Z’)) {
System.out.println(temp + ” 是字母”);
} else {
try {
throw new MyException(str);
} catch (MyException e) {
System.out.println(“异常发生!!!字符串 ” + e.getContent()
+ ” 中由字符 ” + temp + ” 导致异常!” + e.getMessage());
}
}
}
}
public static void main(String[] args) {
TestThrowsException2 example = new TestThrowsException2();
example.check(“aa00bb11cc88dd//ee..ff,,gg–hh==jj((kk))zz”);
}
}
/*
* 创建一个异常类,将其命名为MyException,继承自Exception类。其中定义一个字符串成员content.
* 生成一个构造方法对其赋值。并有一个成员方法取得其值。
*/
@SuppressWarnings(“serial”)
class MyException extends Exception {
private String content;
MyException(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
java编程 泰勒级数 x-x^3/3!+x^5/5!+…
//请采纳!
package com.cainiaoqi;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
double x = 0;
Scanner in = new Scanner(System.in);
System.out.print(“请输入x:”);
x = in.nextDouble();
System.out.println(“sin(“+x+”) = “+calcsin(x));
}
static double calcsin(double x) {
double result = 0;
int temp = 0;
while((Math.pow(x,2*temp+1)/jiecheng(2*temp+1)) 1.0E-8) {
result += (Math.pow(x,2*temp+1)/jiecheng(2*temp+1))*Math.pow(-1, temp);
temp ++;
//System.out.print(“第”+temp+”次迭代!” );
//System.out.println(result);
}
return result;
}
static int jiecheng(int x) {
if(x==1)
return 1;
else
return jiecheng(x-1)*x;
}
}
java编程:利用级数展开式计算cos(x)
spublic class Test{
static public double cos(double x){
int max_n=50; //计算精度,越大越精确
double sum=1;
double sq_x=x*x;
double usum=1;
double lsum=1;
int sign=1;
for(int n=1;nmax_n;n++){
sign*=-1;
usum*=sq_x;
final int dn=2*n, dn_1=2*(n-1);
for(int i=dn;idn_1;i–){
lsum*=i;
}
sum+=sign*usum/lsum;
}
return sum;
}
public static void main(String[] args){
System.out.println(“角度\t\t自制cos函数输出\t\t系统cos函数输出”);
for(int a=0;a=360;a+=10)
System.out.println(a+”\t\t”+cos(Math.toRadians(a))+”\t\t”+Math.cos(Math.toRadians(a)));
}
}
角度 自制cos函数输出 系统cos函数输出
0 1.0 1.0
10 0.9848077530122081 0.984807753012208
20 0.9396926207859085 0.9396926207859084
30 0.8660254037844386 0.8660254037844387
40 0.766044443118978 0.766044443118978
50 0.6427876096865393 0.6427876096865394
60 0.5000000000000001 0.5000000000000001
70 0.3420201433256688 0.3420201433256688
80 0.17364817766693047 0.17364817766693041
90 4.264460367971268E-17 6.123233995736766E-17
100 -0.17364817766693033 -0.1736481776669303
110 -0.3420201433256687 -0.3420201433256687
120 -0.4999999999999999 -0.4999999999999998
130 -0.6427876096865393 -0.6427876096865394
140 -0.766044443118978 -0.7660444431189779
150 -0.8660254037844387 -0.8660254037844387
160 -0.9396926207859084 -0.9396926207859083
170 -0.9848077530122079 -0.984807753012208
180 -1.0000000000000002 -1.0
190 -0.9848077530122081 -0.984807753012208
200 -0.9396926207859081 -0.9396926207859084
210 -0.8660254037844386 -0.8660254037844386
220 -0.7660444431189778 -0.7660444431189781
230 -0.6427876096865386 -0.6427876096865395
240 -0.5000000000000017 -0.5000000000000004
250 -0.3420201433256711 -0.3420201433256694
260 -0.17364817766693233 -0.17364817766693033
270 -1.7589294315635386E-15 -1.8369701987210297E-16
280 0.17364817766692894 0.17364817766692997
290 0.34202014332566927 0.342020143325669
300 0.499999999999999 0.5000000000000001
310 0.6427876096865434 0.6427876096865393
320 0.7660444431189771 0.7660444431189778
330 0.8660254037844417 0.8660254037844384
340 0.939692620785908 0.9396926207859081
350 0.9848077530122056 0.984807753012208
360 0.9999999999999953 1.0
用级数展开式,纯为了展示概念,效率一般不是求cos的最佳方法。
加大max_n的值可以扩大精度。精度50下,求得的值基本和系统函数一致
Java求调和级数,求助
因为你sum是long型,i是int型,在做1/i的时候会做整数除法得到0(1除以大于1的数都得0),并且得到的结果加到sum后还是取整,完全得不到小数位。
因此sum要声明为double型sum = sum+1/i;要改为sum = sum+1.0/i;
if 后面到break;要用大括号括起来
用java编写程序求下列级数的和:1/3+3/5+5/7+7/9+…+97/99
public class Test {
public static void main(String[] args) {
double i, sum = 0;
for(i = 1; i = 97; i += 2){
sum = sum + i / (i + 2);
}
System.out.println(sum);
}
}
java程序 设级数Sn定义为S0=0,S1=1,Sn=S(n-1)+2*S(n-2) (n
public static void main(String[] args) {
int sum = 0;
for(int i = 0;i = 10; i++){
sum += Sn(i);
System.out.println(“S”+i+”:”+Sn(i));
}
System.out.println(“Sn前10项之和:”+sum);
}
//运算
public static int Sn(int num){
int retVla;
if(num=2){
retVla = Sn(num – 1) + 2 * Sn(num – 2);
}else{
retVla = num;
}
}