如何存储商的值,同时除两个变量的数据类型biginteger和int



我试图打印商的值,而将一个BigInteger变量除以一个integer变量,但编译器显示"线程异常"main" java.lang.RuntimeException:不可编译的源代码-二进制操作符'/'的坏操作数类型第一类型: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);
 }
  1. 将int转换为BigInteger
  2. 使用BigInteger。除法来执行运算。(/operand只适用于基本类型)

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

最新更新