Hi
I'm having a problem printing the distinct numbers. after i enter 10 numbers it won't print the distinct numbers instead, it would exit the program right away.Here is my code.
[import java.util.Scanner;
public class problem1 {
public static void main(String[] args) {
// numbers array will store distinct values, maximum is 10
int[] numbers = new int[10];
// how many distinct number are in the array
int numberOfDistinctValues = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter ten numbers: ");
for (int i = 0; i < numbers.length; i++) {
// read an input
int value = input.nextInt();
int j = 0;
for (; j < numberOfDistinctValues; j++) {
if (numbers[j] == value) {
break;
}
}
if (j == numberOfDistinctValues) {
numbers[numberOfDistinctValues] = value;
numberOfDistinctValues++;
}
}
System.out.println("The number of distinct values is " + numberOfDistinctValues);
for (int i = 0; i < numberOfDistinctValues; i++)
System.out.print(numbers[i] + " ");
}
}]
I'm having a problem printing the distinct numbers. after i enter 10 numbers it won't print the distinct numbers instead, it would exit the program right away.Here is my code.
[import java.util.Scanner;
public class problem1 {
public static void main(String[] args) {
// numbers array will store distinct values, maximum is 10
int[] numbers = new int[10];
// how many distinct number are in the array
int numberOfDistinctValues = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter ten numbers: ");
for (int i = 0; i < numbers.length; i++) {
// read an input
int value = input.nextInt();
int j = 0;
for (; j < numberOfDistinctValues; j++) {
if (numbers[j] == value) {
break;
}
}
if (j == numberOfDistinctValues) {
numbers[numberOfDistinctValues] = value;
numberOfDistinctValues++;
}
}
System.out.println("The number of distinct values is " + numberOfDistinctValues);
for (int i = 0; i < numberOfDistinctValues; i++)
System.out.print(numbers[i] + " ");
}
}]