我的这个java程序有错误,请帮我看看。

来源:百度知道 编辑:UC知道 时间:2024/05/25 19:34:50
import java.io.*;

public class Factorial{
public static void main(String []args) throws IOException{
int a=1;

for(a=1;a<=100;a++){
System.out.print("请输入1个100以内的数:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
Factorial fl=new Factorial();
a=Integer.parseInt(s);
System.out.println(fl.Factorial2(a));

}while(a>100){

System.out.print("输入的数大于100!");
}

}

public int Factorial2(int n){

if(n==0){
return 1;
}else{
return n*Factorial2(n-1);
}
}

}

为何运行60啊什么的都是显示0,还有输入100以上会无限显示“输入的数大于100!”,请帮我改改啊?,谢谢
为何60出来的是负数啊,还有循环的问题,我想是出在while(a>100)这句上谢谢

因为int类型的数据范围在-2147483648-2147483647
60的Factorial数列已经超出它的范围,应该改为long类型
代码:
import java.io.*;

public class Factorial{
public static void main(String []args) throws IOException{
long a=1;

for(a=1;a<=100;a++){
System.out.print("请输入1个100以内的数:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
Factorial fl=new Factorial();
//a=Integer.parseInt(s);
a=Long.parseLong(s);
System.out.println(fl.Factorial2(a));

}while(a>100){

System.out.print("输入的数大于100!");
}

}

public long Factorial2(long n){

if(n==0){
return 1;
}else{
return n*Factorial2(n-1);
}
}

}
我一时说不清,扯到计算机文法了,long类型范围我也不清楚,我知道int类型的范围,拿int类型来说吧如果大于它的正最大值,在加几个正数的话它会得到一个负数,不行你可以试一试

while是循环的意思~~ 当然要循环拉~~
你写成
if(a>100)
{
System.o