Chinaunix首页 | 论坛 | 博客
  • 博客访问: 704379
  • 博文数量: 147
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1725
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-22 10:36
文章分类

全部博文(147)

文章存档

2011年(1)

2010年(1)

2009年(35)

2008年(110)

我的朋友

分类: Java

2008-09-07 17:12:49

在java中存在俩个与数学相关的类:java.lang.Math(基本数学运算的类)和java.Math(执行任何精度指数和实数运算的类)
一。java.lang.Math的方法可以参见api文档
例如:pubic static double sin(double a)
......
计算随机数的方法:public static double random()(只能生成0.0-0.1之间的数)
java.util.Random类可以生成一个随机数流,方法很多。
例如:生成1-100之间的随即整数
long answer=Math.round(Math.random()*100);
int answer=Math.abs(new Random().nextInt(101))
int answer=Math.abs(new Random().nextInt()%100)+1
二。java.Math
精确整数的类是BigInteger
精确实数的类是BigDecimal
BigInteger有很多的成员变量和方法 参见api文档
例子:精确计算n!
public class factorialBigIntegerDemo{
static BigInteger factorial(BigInteger x){
if(x.compareTo(BigInteger.ONE)==0)
   return (BigInteger.ONE);
else
  return (x.multiply(factorial(x.subtract(BigInteger.ONE))));
}
pbulic static void main(String args[]){
....
....
}
}
阅读(530) | 评论(0) | 转发(0) |
0

上一篇:不要重复DAO

下一篇:java字符串类

给主人留下些什么吧!~~