简体   繁体   中英

How to exit from a method, i.e how can i return from a function in this recursion in java?

How to exit from a method, ie how can i return from a function in this recursion in 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;


    }
}

Please help me getting out of this method. This is a program to count number of zeroes.

Example, 34029030 ans = 3

You can try below approach:

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

How it works: Assuming your input > 0 , we try to get the last digit of the number by taking the modulus by 10. If it is equal to zero, we add one to the value that we will return. And what will be the value that we would be returning? It will be the number of zeroes present in the remaining number after taking out the last digit of input .

For example, in the below case, 40300: we take out 0 in first step, so we return 1+number of zeroes in 4030. Again, it appears as if we have called our recursive function for the input 4030 now. So, we again return 1+number of zeroes in 403.

In next step, since last number is 3, we simply return 0+total number of zeroes in 40 or simply as total number of zeroes present in 40 and so on.

For ending condition, we check if the input is itself 0. If it is zero then this means that we have exhausted the input number and there are no further numbers to check for. Hence, we return zero in that case. Hope this helps.

If your main focus is to find number of zeroes in a given number , You can use this alternatively:

     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);` `

Instead of posting a code answer to your question, I'll post a few pointers to get you moving.

  1. As @jrahhali said, as your code is, it'll not get past the return statement inside the if block(which is an error BTW, because you have an int return type).

  2. I'd recommend that you move the last two lines to some calling function(such as a main method). That way all this function will need to do is do some basic processing and move forward.

  3. You aren't checking k at all. As it is, your count is going to always increment.

Hope this much is enough for you to figure things out.

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();

}

Method1 :

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

Method2 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM