计算结果在long范围内的阶乘

阶乘的概念就不用说了,比如5!=5*4*3*2*1=120

但因为long的范围是 -2147483648~2147483647,因此这段程序只讨论结果在这范围内的阶乘数

 1 import java.util.Scanner;
 2 
 3 public class FactorialNumber {
 4     public static void main(String[] args) {
 5         // 输入
 6         long result=1;
 7         Scanner sc=new Scanner(System.in);
 8         int num=sc.nextInt();
 9                //循环乘结果,每乘一次n减1
10             for(int n=num;n>0;n--)
11             {
12                 result=result*n;
13             }
14             System.out.println("The Factorial Number Of"+" "+num+" "+"is"+" "+result);
15     }
16 }
一年之后,我会非常庆幸自己今天选择了开始。 当我觉得为时已晚的时候,恰恰是最早的时候。 It does not matter how slow I go so long as I do not stop.
原文地址:https://www.cnblogs.com/techpalm/p/2912652.html