I'm lost as how to compare 100 randomly generated number between 0-9 to an array value, also between 0-9, and then print the results. Be easy on me, I'm new to coding and I know I suck. I feel as though I'm 75% there. I know there are ways to make some of the code less redundant, however I seem to struggle with those techniques.
Here's what I have so far:
public static void main(String[] args) { double randomNum = 0; for (int i = 0; i < 100; i++) { randomNum = Math.random() * 10; int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0; int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0; int [] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for (double j = 0; j <arrayNums.length; j++){ if (arrayNums[0] == randomNum) { count0++; } else if (arrayNums[1] == randomNum){ count1++; } else if (arrayNums[2] == randomNum){ count2++; }else if (arrayNums[3] == randomNum){ count3++; }else if (arrayNums[4] == randomNum){ count4++; }else if (arrayNums[5] == randomNum){ count5++; }else if (arrayNums[6] == randomNum){ count6++; }else if (arrayNums[7] == randomNum){ count7++; }else if (arrayNums[8] == randomNum){ count8++; } else{ count9++; } } System.out.print("Occurrences of 0: " + count0); System.out.print("\nOccurrences of 1: " + count1); System.out.print("\nOccurrences of 2: " + count2); System.out.print("\nOccurrences of 3: " + count3); System.out.print("\nOccurrences of 4: " + count4); System.out.print("\nOccurrences of 5: " + count5); System.out.print("\nOccurrences of 6: " + count6); System.out.print("\nOccurrences of 7: " + count7); System.out.print("\nOccurrences of 8: " + count8); System.out.print("\nOccurrences of 9: " + count9); } } }
Any and all help is appreciated.
arrayNums[1],arrayNums[2]etc. You could use another array (of counts) and ore looping to shrink this to about 10 lines of code.