java - Any idea on how can I count the number of elements that verify an "if" condition? -


private static int chain(int n){         int count = 0;         while(n > 1){             if(n % 2 == 0){                 count++; //the value not stored                 return chain(n/2);             }             count++; //same thing             return chain(3*n+1);         }         return count; //prints initial value (0)     } } 

i need print number of times chain method reoccurs.

remove count variable method, , make static member of class. , prevent repeating yourlsef (dry principle), should increment count variable @ top of method.

private static int count = 0;  private static int chain(int n) {     count++;      while(n > 1) {         if(n % 2 == 0) {             return chain(n/2);         }          return chain(3*n+1);     }  return count; } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -