Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 大整数%(mod)_Java_Biginteger_Mod - Fatal编程技术网

Java 大整数%(mod)

Java 大整数%(mod),java,biginteger,mod,Java,Biginteger,Mod,我的代码的任务是显示一个数字是否与数字1-12相除。如果是,则应打印(“1”),如果不是(“0”) 例如: Input == 6; Output == 111001000000; 问题是“%”不适用于BigInteger。我搜索了一个解决方案,发现BigInteger mod(BigInteger other)应该可以完成这项工作,但我无法真正理解我如何使用它,因为每次我都尝试使用它。它抛出了一个错误 class Main { public static void main(Strin

我的代码的任务是显示一个数字是否与数字1-12相除。如果是,则应打印(“1”),如果不是(“0”)

例如:

Input == 6;
Output == 111001000000;
问题是“%”不适用于BigInteger。我搜索了一个解决方案,发现BigInteger mod(BigInteger other)应该可以完成这项工作,但我无法真正理解我如何使用它,因为每次我都尝试使用它。它抛出了一个错误

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger bi = sc.nextBigInteger();

        for (int i = 1; i <= 12; i++) {
            if (bi % i == 0) {
                System.out.print("1");
            }
            if (bi % i != 0) {
                System.out.print("0");
            }
        }
    }
}
主类{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
BigInteger bi=sc.nextBiginger();

对于(int i=1;i当您使用
Integer
时,jdk将为您自动装箱和自动取消装箱。因此,您可以使用运算符
%
修改它们,就像使用原语值一样。 虽然jdk不会自动装箱或自动取消装箱
biginger
,但必须显式调用
mod
方法

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger bi = sc.nextBigInteger();

        for (int i = 1; i <= 12; i++) {
            if (bi.mod(BigInteger.valueOf(i)).equals(BigInteger.ZERO)) {
                System.out.print("1");
            } else {
                System.out.print("0");
            }
        }
    }
}
主类{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
BigInteger bi=sc.nextBiginger();

对于(int i=1;i什么错误?您没有用
mod
显示非工作代码,但我猜这与尝试使用
int
而不是
biginger
作为
mod
函数的oneliner的参数有关:Stream biStream=LongStream.range(1,12)。mapToObj((i)->biginger.valueOf(i)).map((biginger d)->(bi.mod(d).equals(biginger.ZERO)?“1\n”:“0\n”);