这是显示系列1-(a^2/3!)+(a^4/5!)-(a^6/7!)+.....的总和的程序 我使用递归显示数字的阶乘
import java.io.*;
class Series{
public static void main(String args[])throws IOException{
int n,a;
double sum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter the value of n & a");
str=br.readLine();
n=Integer.parseInt(str);
str=br.readLine();
a=Integer.parseInt(str);
for(int i=0;i<=n;i++){
sum=sum+(Math.pow(-1,i)*(Math.pow(a,2*i)/fact(2*i+1)));
}
System.out.println(sum);
}
static int fact(int n){
int fact=1,i;
for(i=1;i<=n;i++){
fact=fact*i;
}
return(fact);
}
}
输出=
Exception in thread "main" java.lang.NumberFormatException: For input string: "3 6"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Series.main(Series.java:12)
请帮我解决java编程中的错误 线程“main”java.lang.NumberFormatException异常
最佳答案
Integer.parseInt(str);
这是错误的行,如 str="3 6"(在您的情况下),并且无法将其转换为整数。
关于java - java编程错误线程异常 "main"java.lang.NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25349993/