在JAVA中乘以巨大的数字

时间:2015-05-30 13:15:15

标签: java java.util.scanner biginteger

我试图通过添加扫描程序来获取用户输入,但我不能。您可以通过添加Scanner来帮助我改进此代码以获取用户输入。我尝试了但它得到了一些错误。我是JAVA语言的初学者。

import java.math.*;

public class BigIntegerMul {

public static void main(String args[]) {

    BigInteger int1 = new BigInteger("131224324234234234234313");
    BigInteger int2 = new BigInteger("13345663456346435648234313");
    BigInteger result = int1.multiply(int2);
    System.out.println(result);

}
}

1 个答案:

答案 0 :(得分:2)

要使用BigInteger获取Scanner,您可以使用以下代码段 -

import java.math.*;
import java.util.Scanner;

public class BigIntegerMul {

public static void main(String args[]) {

    //BigInteger int1 = new BigInteger("131224324234234234234313");
    //BigInteger int2 = new BigInteger("13345663456346435648234313");

    BigInteger int1, int2, result; 
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter first bigInteger: ");
    int1 = sc.nextBigInteger();

    System.out.println("Enter second bigInteger: ");
    int2 = sc.nextBigInteger();

    result = int1.multiply(int2);

    System.out.println("Result: " +result);

 }
}