1
我正在編寫一個程序來打印系列和系列的總和(接受來自用戶的X和N)。這是系列:Java-Print系列和該系列的總和
S=1-X^2/2!+X^3/3!-X^4/4!....x^N/N!
這是我這麼遠:
import java.io.*;
public class Program6
{
int n,x;
double sum;
public void getValue() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input a value to be the maximum power");
n=Integer.parseInt(br.readLine());
System.out.println("input another value");
x=Integer.parseInt(br.readLine());
}
public void series()
{
sum=1.0;
double fact=1.0;
for(int a=2;a<=n;a++)
{
for(int b=a;b>0;b--)
{fact=fact*b;
}
double c=a/fact;
if(a%2==0)
sum=sum-(Math.pow(x,c));
else
sum=sum+(Math.pow(x,c));
fact=1;
}
}
public void display()
{
System.out.println("The sum of the series is " +sum);
}
public static void main(String args[])throws IOException
{
Program6 obj=new Program6();
obj.getValue();
obj.series();
obj.display();
}
}
我無法弄清楚如何打印系列本身。
這將真正幫助,如果你想縮進代碼 - 也,我強烈建議使用大括號周圍所有'if' /'else'機構 - 它將使代碼更加清晰,特別是當縮進被搞亂時...... –
我認爲Scanner足以從鍵盤讀取值。 –