封装一个方法,求整数n的阶乘

xiaoxiao2021-02-28  52

package task_3_9; /** * 封装一个方法,求整数n的阶乘 */ public class Task01 { public static void main(String[] args) { System.out.println(getJcheng(20)); } /** * 求阶乘方法 * 接收>=0的整数,返回阶乘值(long)类型,(0的阶乘为1) * 如果传入负数,返回0 */ public static long getJcheng(int n) { if(n < 0) { return 0; } long jcVal = 1L; //阶乘值 for(int i=1;i<=n;i++) { jcVal *= i; } return jcVal; } }
转载请注明原文地址: https://www.6miu.com/read-2620206.html

最新回复(0)