我注意到我找不到很大的数字。我决定使用 biginteger 来解决这个问题,但它不允许我将它们分开。我还将其中一个除法变成了 big int 除法方法,但它仍然给了我一个危险信号。谁能帮我弄清楚为什么会发生这种情况?除法也不起作用。我将其中一个除法改为除法,其余保留为常规除法。
//This class test the recursive method to see how many digits are in a number
public class TestDigits {
public static void main(String[] args) {// main method to test the nmbDigits method
Scanner c = new Scanner(System.in);
try{
System.out.println("Input an integer number:");
BigInteger number = c.nextBigInteger() ;
System.out.println(nmbDigits(number));}
catch (InputMismatchException ex){
System.out.println("incorrect input, integer values only.");
System.exit(1);}}
static BigInteger nmbDigits(BigInteger c) {//nmbDigits method takes input from user and returns the number of digits
int digits = 0;
if (c.divide(10) == 0){
digits++;}
else if (c / 10 != 0){
digits++;
BigInteger count = c/10;
do {
count = count/10;
digits++;}
while (count != 0);}
return digits;}
}
最佳答案
您不能在 BigInteger
的实例上使用除法运算符 /
。该运算符仅适用于原始数值类型。这就是 BigInteger
类具有 divide
方法的原因。
BigInteger result = c.divide(new BigInteger("10"));
可以。
关于java - 为什么这个程序不允许我划分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25432714/