【JAVA基础知识】第八章 常用使用类(4)

付费节点推荐


免费节点


节点使用教程


8.6.1 Math类
常用的Math类方法(java.lang包中)  (import java.lang.Math;)
public static lang abs(double a) 返回a的绝对值
public static double max(double a,double b) 返回a,b的最大值
public static double min(double a,double b) 返回a,b的最小值
pulbic static double random() 产生一个0~1之间的随机数(不包括0和1)
public static double pow(double a,double b) 返回a的b次幂
public static double sqrt(double a) 返回a的平方根
public static double log(double a) 返回a的对数
public static double sin(double a) 返回a的正弦
public static double asin(double a) 返回a的反正弦值
8.6.2 BigInteger 类

java.math包中的BigInteger类提供任意精度的整数运算。(import java.math.BigInteger;)

public BigInteger add(BigInteger val)    //求和

public BigInteger subtract(BigInteger val)   //求差

public BigInteger multiplyt(BigInteger val)  // 求积

public BigInteger divide(BigInteger val)    // 求商

public BigInteger remainder(BigInteger val)  // 求余

public  BigInteger abs()    //返回当前大整数对象的绝对值

public BigInteger pow()    //返回当前大整数对象的a次幂

public String  toString()   // 返回当前大整数对象十进制的字符串表示

public String toString(int p)    //返回当前大整数对象p进制的字符串表示

例如:

package com.BigInteger;
import java.lang.Math;
import java.math.BigInteger;

public class example {

public static void main(String[] args) {
double a=Math.sqrt(5);
System.out.println(a);
BigInteger result=new BigInteger(“1”);

BigInteger one=new BigInteger(“123456789067899”);

result=one.add(result);
System.out.println(result);

}

}

8.6.3  Random类

(1)使用Math类中的random()方法放回一个0至1之间的随机数,不包括0和1

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

(2)使用Random类(java.util)的构造方法创建Random对象/

Random random=new Random();

random.nextInt();

random.nextBoolean();

(3)如果想用随机数生成器random返回0~n之间(包括0不包括n)的随机数,可以让random调用带参数的nextInt(int m)方法

random.nextInt(100);

例如:

package com.random;

import java.util.Random;public class example {

public static void main(String args[])
{

System.out.println(Math.random()); // 调用Math.random()放回一个0至1之间的随机小数,不包括0和1

Random random=new Random();
System.out.println(random.nextInt()); //随机产生一个整数
System.out.println(random.nextBoolean()); //随机产生true或false
System.out.println(random.nextDouble()); //随机产生0~1之间的double型数字
System.out.println(random.nextInt(100)); //随机产生一个0~100之间的整数(包括0,不包括100)
}

}

8.7  数字格式化

 

 

 

 

未经允许不得转载:Bcoder资源网 » 【JAVA基础知识】第八章 常用使用类(4)

相关推荐

更多优质资源关注微信公众号: bcoder

bcoder
赞 (0)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册