Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.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 为什么这个程序不允许我分开_Java - Fatal编程技术网

Java 为什么这个程序不允许我分开

Java 为什么这个程序不允许我分开,java,Java,我注意到我找不到真正大的数字。我决定使用biginteger来解决这个问题,但它不允许我将它们除。我还将其中一个分区转换为大int-dividon方法,但它仍然给了我一个危险信号。有人能帮我弄清楚为什么会这样吗?divide方法也不起作用。我将一个除法改为divide方法,剩下的作为常规除法 //This class test the recursive method to see how many digits are in a number public class TestDigits {

我注意到我找不到真正大的数字。我决定使用biginteger来解决这个问题,但它不允许我将它们除。我还将其中一个分区转换为大int-dividon方法,但它仍然给了我一个危险信号。有人能帮我弄清楚为什么会这样吗?divide方法也不起作用。我将一个除法改为divide方法,剩下的作为常规除法

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

不能在
biginger
的实例上使用除法运算符
/
。此运算符仅适用于基本数字类型。这就是为什么
biginger
类有一个
divide
方法


biginger结果=c.divide(新的biginger(“10”)
可以工作。

您不能在
BigInteger
的实例上使用除法运算符
/
。此运算符仅适用于基本数字类型。这就是为什么
biginger
类有一个
divide
方法

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
    long digits = 0;
    if (c.divide(BigInteger.valueOf(10L)) == BigInteger.valueOf(0L)){
        digits++;}
      else if (c.divide(BigInteger.valueOf(10L)) != BigInteger.valueOf(0L)){
        digits++;
        long count = (c.divide(BigInteger.valueOf(10L))).longValue();
        do {
            count = count/10;
            digits++;}
          while (count != 0);}
    return BigInteger.valueOf(digits);}
}

biginger结果=c.divide(新的biginger(“10”)
可以工作。

仅供参考,您可以将整数转换为字符串并获取其长度,以获取位数。该项目的目的是递归方法…………好吧,没有冒犯,但您应该再次查找递归方法
nmbDigits
不是递归的。递归方法对自身进行调用。仅供参考,您可以将整数转换为字符串并获取其长度,以获取位数。该项目的目的是递归方法…………好吧,没有冒犯,但您应该再次查找递归方法
nmbDigits
不是递归的。递归方法对自身进行调用。@user3602515 divide方法接受BigInteger参数并返回BigInteger。你没有正确使用它。所以我不能用一个小数字除以大数字?因此,我只能查找长度小于10位数的较小数字的位数?@user3602515您必须首先将小数字转换为
biginger
。@user3602515 divide方法接受biginger参数并返回biginger。你没有正确使用它。所以我不能用一个小数字除以大数字?因此,我只能找到长度小于10位数的较小数字的位数?@user3602515您必须首先将小数字转换为
biginger
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
    long digits = 0;
    if (c.divide(BigInteger.valueOf(10L)) == BigInteger.valueOf(0L)){
        digits++;}
      else if (c.divide(BigInteger.valueOf(10L)) != BigInteger.valueOf(0L)){
        digits++;
        long count = (c.divide(BigInteger.valueOf(10L))).longValue();
        do {
            count = count/10;
            digits++;}
          while (count != 0);}
    return BigInteger.valueOf(digits);}
}