解题思路:
在 Java 中,有许多数字处理的类,比如 Integer类,但是Integer类有一定的局限性。
我们都知道 Integer 是 Int 的包装类,int 的最大值为 2^31-1。若希望描述更大的整数数据时,使用Integer 数据类型就无法实现了,所以Java中提供了BigInteger 类 ;BigInteger类型的数字范围较Integer,Long类型的数字范围要大得多,它支持任意精度的整数,也就是说在运算中 BigInteger 类型可以准确地表示任何大小的整数值而不会丢失任何信息。
注意事项: Scanner类使用.nextBigInteger()接收 .multiply()为BigInteger的乘法法运算 .add()为BigInteger的加法法运算
//1.加 BigInteger bigNum1 = a.add(b);
//2.减 BigInteger bigNum2 = a.subtract(b);
//3.乘 BigInteger bigNum3 = a.multiply(b);
//4.除 BigInteger bigNum4 = a.divide(b);
//5.取模(需 b > 0,否则出现异常:ArithmeticException("BigInteger: modulus not positive")) BigInteger bigNum5 = a.mod(b);
//6.求余 BigInteger bigNum6 = a.remainder(b);
//7.平方(需 n >= 0,否则出现异常:ArithmeticException("Negative exponent")) BigInteger bigNum7 = a.pow(n);
//8.取绝对值 BigInteger bigNum8 = a.abs(); //13 //9.取相反数 BigInteger bigNum9 = a.negate(); //-13参考代码:
详细代码:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//定义BigInteger类数据a,b,c,d
BigInteger a, b, c, d;
//使用.nextBigInteger()接收
a = sc.nextBigInteger();
b = sc.nextBigInteger();
c = sc.nextBigInteger();
d = sc.nextBigInteger();
// a×d+b×c
//.multiply()为BigInteger的乘法法运算 .add()为BigInteger的加法法运算
/** 下列为分解计算步骤
* BigInteger ad = a.multiply(d);
BigInteger bc = b.multiply(c);
BigInteger ab_add_bc=ad.add(bc);
*/
//一次输出结果
System.out.println((a.multiply(d)).add(b.multiply(c)));
}
}
0.0分
1 人评分
C语言考试练习题_一元二次方程 (C语言代码)浏览:737 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:617 |
打水问题 (C语言代码)浏览:1072 |
程序员的表白 (C语言代码)浏览:667 |
淘淘的名单 (C语言代码)浏览:1104 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:262 |
A+B for Input-Output Practice (VI) (C语言代码)浏览:558 |
有关字符,字符串的输入输出函数说明浏览:479 |
【偶数求和】 (C语言代码)浏览:431 |
C二级辅导-计负均正 (C语言代码)浏览:482 |