Java_认识BigInteger

文章讲述了Java中的BigInteger类,包括其构造函数用于生成随机数和指定基数,以及常用的数学方法如加减乘除、幂运算、比较和类型转换。
摘要由CSDN通过智能技术生成

一.BigInteger的构造

package 基础.week2.day10;

import java.math.BigInteger;
import java.util.Random;

public class Demo1 {
    public static void main(String[] args) {
        //BigInteger的构造
        Random r=new Random();
        BigInteger bigInteger=new BigInteger(4,r);//随机[0~2^n-1]
        System.out.println(bigInteger);

        BigInteger bg1=new BigInteger("123");//常用
        System.out.println(bg1);

        BigInteger bg2=new BigInteger("100",2);//进制
        System.out.println(bg2);

        BigInteger bg3 = BigInteger.valueOf(100);//value of(long num)
        System.out.println(bg3);
    }
}

9
123
4
100

Process finished with exit code 0
 

 二.BigInteger的常用方法

package 基础.week2.day10;

import java.math.BigInteger;

public class Demo2 {
    public static void main(String[] args) {
        //BigInteger的常用方法
        BigInteger bg1=BigInteger.valueOf(10);
        BigInteger bg2=BigInteger.valueOf(5);

        BigInteger bg3 = bg1.add(bg2);
        System.out.println(bg3);//加

        BigInteger bg4=bg1.subtract(bg2);
        System.out.println(bg4);//减

        BigInteger bg5=bg1.multiply(bg2);
        System.out.println(bg5);//乘法

        BigInteger bg6=bg1.divide(bg2);
        System.out.println(bg6);//除法

        BigInteger[] bigIntegers = bg1.divideAndRemainder(bg2);
        System.out.println(bigIntegers[0]);//除法 得到商和余数
        System.out.println(bigIntegers[1]);

        System.out.println(bg1.equals(bg2));//判断是否相同

        BigInteger bg7 = bg1.pow(2);//多少次幂
        System.out.println(bg7);

        BigInteger min = bg1.min(bg2);//最大最小值
        BigInteger max = bg1.max(bg2);
        System.out.println("min:"+min);
        System.out.println("max:"+max);

        BigInteger bg=BigInteger.valueOf(1000);
        int i = bg.intValue();//转为int类型
        System.out.println(i);


    }
}

15
5
50
2
2
0
false
100
min:5
max:10
1000

Process finished with exit code 0
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值