Answer:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] distinctNumbers = new int[10]; // Array of length 10;
int num; // User input
int count = 0; // Number of distinct numbers
// Prompt the user to enter ten numbers
System.out.print("Enter ten numbers: ");
for (int i = 0; i < 10; i++) {
num = input.nextInt();
// Test if num is distinct
if (isDistinct(distinctNumbers, count, num)) {
distinctNumbers[count++] = num;
}
}
// Displays the number of distinct numbers and the // distinct numbers separated
// by exactly one space
System.out.println("The number of distinct numbers is " + count);
System.out.print("The distinct numbers are");
for (int i = 0; i < count; i++) {
System.out.print(" " + distinctNumbers[i]);
}
System.out.println();
}
/** Method isDistinct returns true if number is not in array false otherwise */
public static boolean isDistinct(int[] array, int count, int num) {
for (int i = 0; i < count; i++) {
if (num == array[i])
return false;
}
return true;
}
Step-by-step explanation:
The original program doesn't work if there are zeros entered. This is because arrays are initialized with zeros. If you take the count along everywhere, you can fix this.
The code will become more readable and more robust if you use a collection like a Set or a List.