Can someone explain this code line by line. I just started to learn how to code and I'm learning java right now,but I'm having hard time how this function executes. What does new int[10]
; really do? and I don't really get the difference or how repeatedDigits[index]
part is used differently from the repeatedDigits[remainder]
part.
Write a function that counts the repeated digits in the integer x.
For * example: if x
is 999, then your function must print back that 9 occurs 3 * times
. Another example, if the integer x is 992121, then your function * must print back that 9 occurs 2 times, 2 occurs 2 times and 1 occurs 2 * times
. This was a guideline for the function.
public static void countRepeatedDigitsInANumber(int x) {
int[] repeatedDigits = new int[10];
while (x > 0) {
int remainder = x % 10;
x = x / 10;
repeatedDigits[remainder] = repeatedDigits[remainder] + 1;
}
int index = 0;
while (index < 10) {
if (repeatedDigits[index] != 0)
System.out.println("The digit " + index + " occurs " +
repeatedDigits[index]);
index++;
}
}
This is a code that compiles. I'm just posting this because I'm having hard time to understand. I would realy appreciate if someone could give any suggestion to learn java quickly as well!