mardi 12 novembre 2019

Count number of zeroes in number issue with code

My code is counting the wrong number of zeroes in a read in text file and I'm not sure how to fix it. Random numbers are coming up either one more than what i need or not reading at all. Can anyone help?

private static int count0(int n, boolean zero) {
  if (n <= 0)
    return 0;
  else if (n % 10 == 0)
    return 1 + (zero ? 1 : 0) + count0(n / 10, true);
  else
    return count0(n / 10, false);
}

public static int count0(int n) {
  return count0(n, false);
}

enter code here

Aucun commentaire:

Enregistrer un commentaire