java - JAVA中的大数相乘

标签 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);

}
}

最佳答案

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

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);

 }
}

关于java - JAVA中的大数相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30546650/

相关文章:

java - Bruce Eckel 的 "Thinking in Java"中的这个泛型示例是否错误?

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - Java Biginteger 在 pow 中的最大值

java - 如何在 Java 中将两个非常大的数字相加,无论其大小如何,而不使用 BigInteger 数据类型?

sse - 实用的 BigNum AVX/SSE 可能吗?

java - 使用 Mockito 验证发送到未在模拟类中直接调用的方法的参数

java - 在 Eclipse 中检查 java 类实例变量值

java - 字符串修剪功能不起作用

Java - 从文件中读取有符号整数

java - 文件扫描器 : Unreported exception FileNotFoundException; must be caught or declared to be thrown