Milan
Milan
JCHJava Community | Help. Code. Learn.
Created by Milan on 11/28/2024 in #java-help
countDigits I need help
Hello I'm studying Computer Science and cant solve this problem right. Write a method that counts how many times a specific digit a appears in the decimal representation of a number b. It is guaranteed that the number b and the digit a are both greater than or equal to 0, with a being at most 9. Even if b = 0, the method should correctly determine whether and how often the digit a appears. My solution: public static int countDigit(long a, int b) { // counts how often the digit from b is contained in the number from l. return (a == 0 && b == 0) ? 1 : (a == 0) ? 0 : (a % 10 == b ? 1 : 0) + countDigit(a / 10, b); } We have to solve this problem with a recursively defined method Can anyone help me out ? ^^
11 replies