Java学习日志14.14(第一阶段基础)

2018.10.30 23:34

14.14_常见对象(BigInteger类的概述和方法使用)

** A:BigInteger的概述
* 可以让超过Integer范围内的数据进行运算

  • B:构造方法
    • public BigInteger(String val)
  • C:成员方法
    • public BigInteger add(BigInteger val)

    • public BigInteger subtract(BigInteger val)

    • public BigInteger multiply(BigInteger val)

    • public BigInteger divide(BigInteger val)

    • public BigInteger[] divideAndRemainder(BigInteger val)
      divideAndRemainder(BigInteger val)
      返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。

      代码练习:
      
package com.heima.otherclass;

import java.math.BigInteger;

public class Demo_Biginteger {
	public static void main(String[] args) {
		//int a = 12345678912;	//超出int存储范围
		//long b = 12345678912;	//超出long的存储范围
		BigInteger bt1 = new BigInteger("456");	
		BigInteger bt2 = new BigInteger("123");	
		System.out.println("两个bigInteger数为:"+ bt1 +"和" + bt2);
		BigInteger bt3 = bt1.add(bt2);		//加
		System.out.println("两者之和:" + bt3);
		System.out.println("_________");
		BigInteger bt7 = bt1.subtract(bt2);	//减
		System.out.println("两者之差:" + bt7);
		System.out.println("_________");
		BigInteger bt4 = bt1.divide(bt2);	//除
		System.out.println("两者之商:" + bt4);
		System.out.println("_________");
		BigInteger bt5 = bt1.multiply(bt2);	//积
		System.out.println("两者之积:" + bt5);
		System.out.println("_________");
		BigInteger []bt6 = bt1.divideAndRemainder(bt2); //求余和除数(商)
		System.out.println("两者的商以及余数:" );
		for (int i = 0; i < bt6.length; i++) {
			System.out.println(bt6[i]);
		}
		
		}
}

程序结果:
两个bigInteger数为:456和123
两者之和:579


两者之差:333


两者之商:3


两者之积:56088


两者的商以及余数:
3
87

猜你喜欢

转载自blog.csdn.net/binge_kong/article/details/83552030