余英奇


私信TA

用户名:KikiTodd

访问量:717

签 名:

等  级
排  名 1428
经  验 2788
参赛次数 3
文章发表 3
年  龄 20
在职情况 学生
学  校 鄂州职业大学
专  业

  自我简介:

解题思路:

                    在 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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区