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高手解答!
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高手解答!
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);
}
}
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;
}
}
}