Java 大整数%(mod)
我的代码的任务是显示一个数字是否与数字1-12相除。如果是,则应打印(“1”),如果不是(“0”) 例如: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
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”);