|NO.Z.00013|——————————|BigDataEnd|——|Java&核心类库.V13|——|Java.v13|Biglnteger类|概念使用|

一、Biglnteger类的概念和使用
### --- 基本概念

~~~     ——>       若希望表示比long类型范围还大的整数数据,
~~~     ——>       则需要借助java.math.BigInteger类型描述。
二、常用的方法
方法声明 功能介绍
BigInteger(String val) 根据参数指定的字符串来构造对象
BigInteger add(BigInteger val) 用于实现加法运算
BigInteger subtract(BigInteger val) 用于实现减法运算
BigInteger multiply(BigInteger val) 用于实现乘法运算
BigInteger divide(BigInteger val) 用于实现除法运算
BigInteger remainder(BigInteger val) 用于实现取余运算
BigInteger[] divideAndRemainder(BigInteger val)  用于实现取商和余数的运算
三、编程代码
package com.yanqi.task11;

import java.math.BigInteger;

public class BigIntegerTest {

    public static void main(String[] args) {

        // 1.构造两个BigInteger类型的对象并指定初始值
        BigInteger bi1 = new BigInteger("20");
        BigInteger bi2 = new BigInteger("8");
        // 2.实现加减乘除取余操作并打印
        System.out.println("实现加法运算的结果是:" + bi1.add(bi2)); // 28
        System.out.println("实现减法运算的结果是:" + bi1.subtract(bi2)); // 12
        System.out.println("实现乘法运算的结果是:" + bi1.multiply(bi2)); // 160
        System.out.println("实现除法运算的结果是:" + bi1.divide(bi2)); // 2
        System.out.println("实现取余运算的结果是:" + bi1.remainder(bi2)); // 4

        System.out.println("-----------------------------------------------------");
        // 3.一次性得到商和余数
        BigInteger[] arr = bi1.divideAndRemainder(bi2);
        for (int i = 0; i < arr.length; i++) {
            System.out.println("下标为" + i + "的元素是:" + arr[i]); // 2 4
        }
    }
}
四、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=50549:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task11.BigIntegerTest
实现加法运算的结果是:28
实现减法运算的结果是:12
实现乘法运算的结果是:160
实现除法运算的结果是:2
实现取余运算的结果是:4
-----------------------------------------------------
下标为0的元素是:2
下标为1的元素是:4

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(12)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示