代码之家  ›  专栏  ›  技术社区  ›  Raghav Sharma

如何退出方法,即如何在java中从递归中的函数返回?

  •  0
  • Raghav Sharma  · 技术社区  · 8 年前

    如何退出方法,即如何在java中从递归中的函数返回?

    public class solution {
    
        public static int countZerosRec(int input){
    
          int n=0;
          int k =0;
          int count=0;
          //Base case
          if(n==0)
          {
            return; // How can i return the method from here, i.e how can i stop the execution of the recursive program now.
          }
    
          k=input%10;
          count++;
          n=input/10;
          countZerosRec(n);
          int myans=count;
          return myans;
    
    
        }
    }
    

    请帮我摆脱这种方法。 这是一个计算零数的程序。

    例如,34029030 ans=3

    5 回复  |  直到 8 年前
        1
  •  2
  •   CodeHunter    8 年前

    您可以尝试以下方法:

    public class MyClass {
        public static void main(String args[]) {
    
    
            System.out.println("total zeroes = " + returnZeroesCount(40300));
        }
        public static int returnZeroesCount(int input){
            if(input == 0)
                return 0;
            int n = input % 10;
            return n == 0 ? 1 + returnZeroesCount(input / 10) : returnZeroesCount(input / 10);
        }
    }
    

    工作原理: 假设您的 input > 0 ,我们尝试通过取模10来获得数字的最后一位。如果它等于零,我们在返回的值上加一。我们将返回的价值是什么?它将是在去掉 input .

    例如,在下面的情况下,40300:我们在第一步中去掉0,所以我们在4030中返回1+0。同样,似乎我们现在已经为输入4030调用了递归函数。所以,我们再次在403中返回1+个零。

    在下一步中,由于最后一个数字是3,我们只需返回0+40中零的总数,或者只返回40中零的总数,依此类推。

    对于结束条件,我们检查输入本身是否为0。如果它是零,那么这意味着我们已经用完了输入的数字,没有更多的数字要检查。因此,在这种情况下,我们返回零。希望这有帮助。

        2
  •  0
  •   Novice    8 年前

    如果您的主要关注点是在给定的数字中查找零的数量,您可以使用以下替代方法:

         int numOfZeroes =0;
         long example = 670880930;
         String zeroCounter = String.valueOf(example);
         for(int i=0; i< example.length();i++){
             if(zeroCounter.charAt(i) ==0){
                 numOfZeroes++;
             }
    
         }
          System.out.print("Num of Zeros are"+ numOfZeroes);` `
    
        3
  •  0
  •   cst1992    8 年前

    我不会对你的问题给出代码式的答案,我会给出一些建议,让你有所行动。

    1. 正如@jrahhali所说,正如您的代码所示,它不会通过 return 中的语句 if 块(顺便说一句,这是一个错误,因为您有 int 回来 类型)。

    2. 我建议你把最后两行移到某个电话 功能(例如 main 方法)。这样,所有这些功能将 需要做的是做一些基本的处理并向前推进。

    3. 你没有检查 k 完全事实上,你的 count 将要 始终递增。

    希望这足够让你把事情弄清楚。

        4
  •  0
  •   suhasini    8 年前
    int count =0;
    private int getZeroCount(int num){
             if(num+"".length == 1){
                      if(num==0){
                            count++;
                      }
                      return count;
             }
             if(num%10 == 0){
                      count++;
             }
             num /= 10;
             getZeroCount();
    

    }

        5
  •  -2
  •   telami    8 年前

    方法1:

    public static int countZero1(int input) {
        int count = 0;
        //The loop takes the remainder for the value of the input, and if it is divided by 10, then its number of digits is 0.
        // When the value of the input is less than 0, the cycle ends
        while (input >0){
            if (input % 10 == 0){
                count ++;
            }
            input /= 10;
        }
        return count;
    }
    

    方法2:

    private static int count = 0;
    public static int countZero2(int input) {
        //Recursive call function
        if (input % 10 == 0){
            count ++;
        }
        input /= 10;
        if (input <= 0){
            return count;
        }else {
            return countZero2(input);
        }
    }