(Print distinct numbers) Write a program that reads in 10 numbers and displays the number of distinct numbers and the distinct numbers in their input order and separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers.
this is what i have so far
public class Exercise07_05 {
public static void main(String[] args) {
// numbers array will store distinct values, maximum is 10
int[] numbers = new int[10];
// How many distinct numbers are in the array
int numberOfDistinctValues = 0;
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter ten numbers: ");
for (int i = 0; i < numbers.length; i++) {
// Read an input
int value = input.nextInt();
boolean isinthearray = false;
for(int j=0; j < numberofdistinctvalue; j++) {
if (value == number[j]) {
isinthearray = true;
}
}
if(!isinthearray){
number[numberofdistinctcalues] =value;
numberofdistinctvalues++;
}
}
System.out.println("The number of distinct numbers is " + count);
System.out.print("The distinct numbers are");
for (int i = 0; i < distinctNumbers.length; i++) {
if (distinctNumbers[i] > 0)
System.out.print(" " + distinctNumbers[i]);
}
System.out.println();
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images