划分两个数据类型BigInteger和int的变量时如何存储商的值?
How do store the value of quotient while dividing two variables of dataypes BigIntger and int?
我试图在将 BigInteger 变量除以整数变量时打印商的值,但编译器显示 "Exception in thread "main" java.lang.RuntimeException: 无法编译的源代码 - 二进制的错误操作数类型operator '/' 第一种:java.math.BigInteger 第二种:int
public static void main(String[] args) {
String s;
BigInteger n, repeat, remainder;
Scanner in=new Scanner(System.in);
s=in.nextLine();
n=in.nextBigInteger();
repeat=n/s.length();
System.out.println(repeat);
}
- 将 int 转换为 BigInteger。
使用BigInteger.divide方法进行运算。 (/ 操作数仅适用于原始类型。)
import java.math.BigInteger;
import java.util.Scanner;
public class ModuloTest {
public static void main(String[] args) {
String s;
BigInteger n, repeat, remainder;
Scanner in = new Scanner(System.in);
s = in.nextLine();
n = in.nextBigInteger();
BigInteger length = BigInteger.valueOf(s.length());
repeat = n.divide(length);
System.out.println(repeat);
}
}
我试图在将 BigInteger 变量除以整数变量时打印商的值,但编译器显示 "Exception in thread "main" java.lang.RuntimeException: 无法编译的源代码 - 二进制的错误操作数类型operator '/' 第一种:java.math.BigInteger 第二种:int
public static void main(String[] args) {
String s;
BigInteger n, repeat, remainder;
Scanner in=new Scanner(System.in);
s=in.nextLine();
n=in.nextBigInteger();
repeat=n/s.length();
System.out.println(repeat);
}
- 将 int 转换为 BigInteger。
使用BigInteger.divide方法进行运算。 (/ 操作数仅适用于原始类型。)
import java.math.BigInteger; import java.util.Scanner; public class ModuloTest { public static void main(String[] args) { String s; BigInteger n, repeat, remainder; Scanner in = new Scanner(System.in); s = in.nextLine(); n = in.nextBigInteger(); BigInteger length = BigInteger.valueOf(s.length()); repeat = n.divide(length); System.out.println(repeat); } }