import java.io.*;
public class Pnx {
public static void main(String[] args){
double x=0;
int n=0;
System.out.println("which item do you want know:");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.valueOf(br.readLine());
x=Integer.valueOf(br.readLine());
}catch(IOException ex){}
System.out.println("the nth item of pnx is:"+p(n,x));
}
static double p(int n,double x)
{
if(n==0)
return 1;
else if(n==1)
return x;
else if(n>1)
return ((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n;
}
}
这是求n阶勒让德多项式的值的代码。在eclipse中编译后说static double p(int n,double x)这里的返回值应该是double类型的。。我这个难道不是double类型的么?
我是java的初学者,所以不太清楚这个。。求解决。。
[code="java"]
static double p(int n, double x)
{
if (n == 0)
return 1D;
else if (n == 1)
return x;
else if (n > 1)
return ((2 * n - 1) * x * p(n - 1, x) - (n - 1) * p(n - 2, x)) / n;
else throw new IllegalArgumentException("n must be positive number");
}
[/code]
楼上正解。