java factorial_Java Longs.factorial(int n)用法及代码示例
Guava LongMath类的factorial(int n)方法返回前n个正整数的乘积,即n!。
用法:
public static long factorial(int n)
参数:该方法仅接受一个整数类型的参数n,用于查找阶乘。
返回值:此方法返回以下值:
如果n为0,则此方法返回1。
如果结果适合长整数,则此方法返回前n个正整数的乘积。
如果结果不适合长时间,则此方法返回Long.MAX_VALUE。
异常:如果n为负数,则factorial(int n)方法将引发IllegalArgumentException。
下面的程序说明LongMath.factorial()方法的使用:
范例1:
// Java code to show implementation of
// factorial(int n) method of Guava's
// LongMath Class
import java.math.RoundingMode;
import com.google.common.math.LongMath;
class GFG {
// Driver code
public static void main(String args[])
{
int n1 = 10;
// Using factorial(int n) method of
// Guava's LongMath class
long ans1 = LongMath.factorial(n1);
System.out.println("factorial of "
+ n1 + " is:"
+ ans1);
int n2 = 12;
// Using factorial(int n) method of
// Guava's LongMath class
long ans2 = LongMath.factorial(n2);
System.out.println("factorial of "
+ n2 + " is:"
+ ans2);
}
}
输出:
factorial of 10 is:3628800
factorial of 12 is:479001600
范例2:
// Java code to show implementation of
// factorial(int n) method of Guava's
// LongMath Class
import java.math.RoundingMode;
import com.google.common.math.LongMath;
class GFG {
static long findFact(int n)
{
try {
// Using factorial(int n) method of
// Guava's LongMath class
// This should throw "IllegalArgumentException"
// as n < 0
long ans = LongMath.factorial(n);
// Return the answer
return ans;
}
catch (Exception e) {
System.out.println(e);
return -1;
}
}
// Driver code
public static void main(String args[])
{
int n = -5;
try {
// Function calling
findFact(n);
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
java.lang.IllegalArgumentException:n (-5) must be >= 0
java factorial_Java Longs.factorial(int n)用法及代码示例相关推荐
- java实现stack search_Java Stack search()用法及代码示例
Java中的java.util.Stack.search(Object element)方法用于搜索堆栈中的元素并获取其与顶部的距离.此方法从1开始而不是从0开始计数位置.位于堆栈顶部的元素被视为在位 ...
- java map.put map_Java Map put()用法及代码示例
此方法用于将指定值与该映射中的指定键相关联. 用法: V put(K key, V value) 参数:此方法有两个参数,key和value,其中key是左参数,value是映射中key的对应值. 返 ...
- java dictionary 实例化_Java Dictionary put()用法及代码示例
字典的put()方法用于在字典中插入映射.这意味着可以将特定键及其值映射到特定字典中. 用法: DICTIONARY.put(key, value) 参数:该方法有两个参数,都属于Dictionary ...
- java math.sin()_Java Math sin()用法及代码示例
java.lang.Math.sin()返回介于0.0和pi之间的角度的三角正弦.如果参数为NaN或无穷大,则结果为NaN.如果自变量为零,则结果为零,其符号与自变量相同.返回的值将在-1和1之间. ...
- java compareandset 包_Java AtomicReferenceArray compareAndSet()用法及代码示例
如果AtomicReferenceArray对象的索引i处的当前值等于期望值,则使用AtomicReferenceArray类的compareAndSet()方法原子地将AtomicReference ...
- java collator_Java Collator compare(String, String)用法及代码示例
java.text.Collator类的compare()方法用于比较两个字符串的强度,并根据结果返回0,正值和负值作为输出. 用法: public abstract int compare(St ...
- java hashcode 例子_Java UUID hashCode()用法及代码示例
Java中UUID类的hashCode()方法通常用于获取UUID的哈希码值. 用法: public int hashCode() 参数:此方法不带任何参数. 返回值:此方法返回一个整数值,该值是此U ...
- java获取年份getyear_Java LocalDateTime getYear()用法及代码示例
LocalDateTime类的getYear()方法用于返回year字段.此方法将MIN_YEAR到MAX_YEAR的Year的原始int值返回. 用法: public int getYear() 参 ...
- java day of month_Java LocalDateTime getDayOfMonth()用法及代码示例
LocalDateTime类的getDayOfMonth()方法用于返回day-of-month字段.此方法返回一个介于1到31之间的整数值,即一个月的日期. 用法: public int getDa ...
最新文章
- 计算机导论成绩分为几部分,计算机导论复习题(选择部分)汇总.docx
- Socket拉屎模型之epoll
- node+express+mongDB实现简单登录注册
- 看看50万码农怎么评论:为什么程序员工资那么高但很少有人炫富?
- zoj 3747 (DP)(连续至多,连续至少)
- 对Linux 目录的认识
- excel高级筛选怎么用_excel中使用高级筛选7个示例、多个条件、复制到另一个工作表公式...
- 【Linux】索引式文件系统
- wx-jq:一套完全原创的微信小程序插件集合库
- linux man手册更新,linux man手册介绍【manual】
- 图论(Tarjan算法与无向图)
- 风力摆控制系统赛题分析
- 极尚“在一起”高层Q2定向会,共谋契机 擎画未来
- 日常猜幸运数字小游戏
- 如何使用苹果官方文档
- 【微信小程序】生命周期
- springboot+jsp人事工资管理系统 java ssm maven
- 用新的思维方式去解决问题
- 周志明虚拟机最新版,大厂面试必备宝典
- 软件开发人员绩效评价标准