如何修复与 BigInteger 和 int 不兼容的操作数

问题描述 投票:0回答:0

我正在尝试为 collatz 猜想创建一个程序,我需要它来处理更大的数字。所以,我尝试使用 BigInteger 类,但它似乎遇到了两种类型的错误:bigint 和 int。

我已经查过big int的问题,它似乎适用于加法和乘法。但是,在检查它是否等于 number(1) 或不等于它时,会出现问题。我尝试使用 num.not(new BigInteger("1")。但这也不起作用。

import java.math.BigInteger;
import java.math.Bigdecimal;
import java.util.Objects;
import java.util.Scanner;
public class Main {
    /**
     * @param args
     */
    public static void main(String[] args) {
        boolean repeat = true;
        while (repeat){
            int i = 0;
            Scanner intIn = new Scanner(System.in);
            Scanner strIn = new Scanner(System.in);
            System.out.println("Enter number you want solved, Don't type 0. Use this program with caution");
            BigInteger num = intIn.nextBigInteger();
//    Error appears to occur here, in line 19.
//    I am unsure how to check inequalities with BigInteger, so that is my current issue
            while (num != 1) {
                if (num.equals(new BigInteger("1"))){

                } else if (num.remainder(new BigInteger("2")) == 0) {
                    num = num.divide(new BigInteger("2"));
                } else {
                    num = num.multiply(new BigInteger("3"));
                    num = num.add(new BigInteger("1"));
                }
                i += 1;
            }
            System.out.println("It took " + i + " steps to reach 1. Would you like to do another number? (yes/no)");
            String yn = strIn.nextLine();
            if (Objects.equals(yn, "no")){
                repeat = false;
            }
        }
    }
}

确切错误:线程“主”java.lang.Error 中的异常:未解决的编译问题: 不兼容的操作数类型 BigInteger 和 int

不兼容的操作数类型 BigInteger 和 int

不兼容的操作数类型 BigInteger 和 int

在 Main.main(Main.java:19)

java math biginteger collatz
© www.soinside.com 2019 - 2024. All rights reserved.