import java.io.*;
class jiecheng 
{
public static void main(String[] args) 
{
int n,sum;

try
{
System.out.println("输入n:");
n=(int)System.in.read();
//n=System.in.read();
}
catch (IOException e)
{
} sum=funtion(n);
System.out.println("The sum is:"+sum);  int funtion(int a)
{
if (a==1) return 1;
    return a*funtion(a-1);
} }
}

解决方案 »

  1.   


    import java.util.Scanner;public class jiecheng {    static int n = 0, sum = 1, result = 1;    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("please n:");
            n = sc.nextInt();        sum = funtion(n);
            System.out.println("The sum is:" + sum);    }    static int funtion(int a) {
            if (a == 0)
                return 0;
            result *= a;
            a--;
            if (a != 0)
                funtion(a);
            return result;
        }
    }
      

  2.   

    funtion方法应该定义static 方法才可以被main方法调用的吧
      

  3.   

    n=(int)System.in.read(); 
      

  4.   

    n没有初值而且n读到的也只是你输入的第一个字符的编码,例如你输入95,则n的值是'9'的值。还有,数学上规定0的阶乘是1,不然你求出来的都是0