java 引入数学类_Java数学类

Java数学类

java.lang.Math

定义了一些与数学函数相关的方法

package com.wkcto.chapter04.math;

/**

* Math类

* @author 蛙课网

*

*/

public class Test01 {

public static void main(String[] args) {

//1) Math.random() 产生[0,1)之间 随机小数

for(int i = 0 ; i<10 ; i++){

System.out.println( Math.random() );

}

//2)

System.out.println( Math.sqrt(100)); //平方根

System.out.println( Math.cbrt(100));//立方根

System.out.println( Math.pow(3, 4));//3的4次方

//3)

System.out.println( Math.ceil( 5.6 )); //返回大于等于指定数的最小整数 6.0

System.out.println( Math.floor( 5.6 ));//返回小于等于指定数的最大整数 5.0

//4)两个常量

System.out.println( Math.PI);

System.out.println( Math.E);

}

}

java.text.DecimalFormat类

package com.wkcto.chapter04.math;

import java.text.DecimalFormat;

/**

* DecimalFormat类

* 对数字格式化

* #任意数字

* 0任意数字, 不足的位置会补0

* .小数点

* ,千分位

* @author 蛙课网

*

*/

public class Test02 {

public static void main(String[] args) {

DecimalFormat decimalFormat = new DecimalFormat("###,###.0000");

System.out.println( decimalFormat.format(12345.67)); //12,345.6700

}

}

BigInteger/BigDecimal类

在进行科学计算/财务计算时, 对精度要求比较高,就使用这两个类

package com.wkcto.chapter04.math;

import java.math.BigDecimal;

import java.math.BigInteger;

import java.math.RoundingMode;

/**

* BigInteger/BigDecimal

* @author 蛙课网

*

*/

public class Test03 {

public static void main(String[] args) {

BigInteger i1 = new BigInteger("123456789123456789123456798132456798132456798132456798456");

BigInteger i2 = new BigInteger("123456789123456789123456798132456798132456798132456798");

//相加

BigInteger i3 = i1.add(i2);

System.out.println( i3 );

//相减

i3 = i1.subtract(i2);

System.out.println( i3 );

//相乘

i3 = i1.multiply(i2);

System.out.println( i3 );

BigDecimal d1 = new BigDecimal("1234567891234567891234567981324567981324567981324567984561.32465798");

BigDecimal d2 = new BigDecimal("12345678912345678912345679813245679813245679813245679841.32465798");

//相除, 小数相除时, 可能会出现除不断的情况, 会产生异常

//BigDecimal d3 = d1.divide(d2);// java.lang.ArithmeticException:

//小数相除时, 可以指定小数的处理方式

BigDecimal d3 = d1.divide(d2, RoundingMode.CEILING);

System.out.println( d3 );

}

}

java.util.Random类

用于产生随机数

package com.wkcto.chapter04.math;

import java.util.Random;

/**

* Random类

* 用于产生随机数

* @author 蛙课网

*

*/

public class Test04 {

public static void main(String[] args) {

//创建Random对象

Random random = new Random();

//产生随机小数, [0,1)

for( int i = 1; i<=10; i++){

System.out.println( random.nextDouble());

}

System.out.println( "-----------------");

//产生随机整数,

for( int i = 1; i<=10; i++){

System.out.println( random.nextInt());

}

System.out.println( "-----------------");

//产生随机整数,指定一个上限, 如[0,100)范围的随机整数

for( int i = 1; i<=10; i++){

System.out.println( random.nextInt(100));

}

System.out.println( "-----------------");

//随机数,可以指定一个随机数的种子, 种子相同的随机数对象,产生随机数序列也是一样的

Random random1 = new Random(45679);

Random random2 = new Random(45679);

// 使用相同种子的随机数对象 产生随机小数, [0,1)

for (int i = 1; i <= 10; i++) {

System.out.println(random1.nextDouble());

}

System.out.println("=====================");

for (int i = 1; i <= 10; i++) {

System.out.println(random2.nextDouble());

}

}

}

总结

掌握String对象的创建(构造方法)

熟练掌握String类的基本操作(常用方法)

理解String对象的不可变性

记住StringBuilder/StringBuffer的特点

记住包装类有哪些

理解为什么提供包装类

掌握创建包装类对象的方法(构造方法)

掌握包装类的基本操作(常用方法)

掌握int/String/Integer之间的相互转换

理解装箱与拆箱, -128~127之间的整数装箱采用享元模式

掌握Date/Calendar对象的创建, 掌握把Date对象以指定的字符串显示, 把日期字符串转换为Date对象

掌握产生随机数的方法

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值