public class fac {    public static void main(String[] args) {
        System.out.println("Please enter a int number:");
        int b = 0;
        try {
            b = System.in.read();
            System.out.println(b);
            b = b - '0';
            System.out.println(b);
        } catch (Exception ex) {
            ex.printStackTrace();
        }        System.out.println(mathod(b));
    }    public static long mathod(int a) {
        return a == 1 ? 1 : a * mathod(a - 1);
    }
}这是一个高手的改后的
   另外一个高手说,输入大于9的数字,就 费了,请第3高手解答!

解决方案 »

  1.   

    我向知道 0的ASCII 和 10的ASCII
      

  2.   

    呵呵!好像是我给你写的吧!那个只是提供一个思路,下面这个可以用,不过也是个原型,思路而已。import java.io.BufferedReader;
    import java.io.InputStreamReader;public class TestInput {  public static void main(String[] args) {
        System.out.println("Please   enter   a   int   number:");
        int b = 0;
        try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
          String line = reader.readLine();
          b = Integer.parseInt(line);
          System.out.println(b);
        } catch (Exception ex) {
          ex.printStackTrace();
        }    System.out.println(mathod(b));
      }  public static long mathod(int a) {
        return a == 1 ? 1 : a * mathod(a - 1);
      }
    }
      

  3.   

    这个程序有好多的问题,特别是计算阶乘那一块,连负数都没有判断,重新写了一个参考一下。import java.math.BigInteger;
    import java.util.Scanner;public class TestBean {    public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int num = 0;
            while (true) {
                try {
                    System.out.print("Please enter a int number: ");
                    num = scanner.nextInt();
                    break;
                } catch (Exception e) {
                    System.out.println("** ERROR, entry again please **");
                    scanner.next();
                }
            }
            System.out.println(num + " != " + method(num));
        }    public static BigInteger method(int a) {
            if (a < 0) {
                throw new IllegalArgumentException("参数错误,不能为负数!");
            }
            if (a <= 20) {
                long result = 1;
                while (a > 1) {
                    result *= a--;
                }
                return new BigInteger(result + "");
            } else {
                BigInteger result = new BigInteger("1");
                while (a > 1) {
                    result = result.multiply(new BigInteger("" + a--));
                }
                return result;
            }
        }
    }