import java.util.Random;
public class Hi {
public static void main(String[] args) {
Random random = new Random();
int integer = random.nextInt(100 - 0) + 0;
System.out.println(sumDigits(integer));
}
public static int sumDigits(int n) {
int sumOfInteger = 0;
while (n != 0) {
sumOfInteger += n % 10;
n /= 10;
}
return sumOfInteger;
}
}
I need to use arrays instead of regular. This is what I have to do Program that generates 100 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, . . . , 9s.) I am getting single numbers but not the count. But at the same time I have to use arrays.
import java.util.Arrays;
import java.util.Random;
public class Hw7Problem3 {
public static void main(String[] args) {
System.out.println(sumDigits());
}
public static int[] sumDigits(int random) {
int[] digits = new int[10];
for (int i = 0; i < 100; i++){
digits[(int) (Math.random() * 10)]++;
}
return digits;
}
}