[求助]看看这个程序错在哪里
题目:计算 式(1):y=x+x/2+x/3+........+x/n;式 (2): y=x+x/2!+x/3!+.......+x/n!;
要求:(1)x为实数,n为正整数。(2)任意输入当n为奇数时,按照式1计算,当n为偶数时,按照式2计算。(3)利用继承机制编程。
说明:我编译通过,但执行结果不对,找不出是哪里错了,请各位同行多多指点!
import java.io.*;
class super_s{
double s;
void eu(double x){s=x;
}
}
class sub_s1 extends super_s{
double eu_1(double x,int n){
double sum=0.0;
int i=1;
super.eu(x);
for(;i<=n;i++)
sum=sum+1.0/i;
s=s*sum;
return(s);
}
}
class sub_s2 extends super_s{
double eu_2(double x,int n){
double mul=1.0,sum=0.0;
int i=1;
super.eu(x);
for(;i<n;i++)
{sum=sum+mul;
mul=mul*1.0/(i+1);
}
s=s*sum;
return(s);
}
}
public class out {
public static void main(String arg[]){
int n=0;
double x=2.0,result=0.0;
sub_s1 s1=new sub_s1();
sub_s2 s2=new sub_s2();
System.out.println("please enter a number n:");
try{n=(int)System.in.read();
}
catch(IOException e){System.err.println(e.toString());
}
n=n-48;
if(n%2==1)
result=s1.eu_1(x,n);
else
result=s2.eu_2(x,n);
System.out.println("result="+result);
}
}