i have an assignment , i have been working on it all day, i finished it, and it works, but not in the way my teacher wants it, i have been racking my brain for hours without any luck
this is the assignment question
"write a program that inputs five each of which is between 10 and 100 inclusive,as each number i read, display it only if it is not a duplicate number already read, provide for the "worst case," in which all five numbers are different, use the smallest possible array to solve this problem. Display the complete set of unique values input after the user inputs each new value."
this is the code i wrote
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sid[] = new int[5];
int count = 0;
int x = 0;
int num = 0;
while (x < sid.length)
{
System.out.println("Enter number: ");
num = input.nextInt();
if ((num >= 10) && (num <= 100)) {
boolean digit = false;
x++;
for (int i = 0; i < sid.length; i++)
{ if (sid[i] == num)
digit = true;
}
if (!digit) {
sid[count] = num;
count++;
}
else
System.out.printf("the number was entered before \n");
}
else
System.out.println("number must be between 10 and 100");
for (int i =0; i < x; i++) {
System.out.print(sid[i] +" ");
}
System.out.println();
}
}
}
my problem is in the output the output is like this
{
Enter number:
11
11
Enter number:
21
11 21
Enter number:
34
11 21 34
Enter number:
11
the number was entered before
11 21 34 0
Enter number:
44
11 21 34 44 0
}
But my teacher wants the output too look like this, without the 0's at the end, and without changing the value of the array sid from sid[5] to sid[4]
{
Enter number:
11
11
Enter number:
21
11 21
Enter number:
34
11 21 34
Enter number:
11
the number was entered before
11 21 34
Enter number:
44
11 21 34 44
}
i have been trying for hours now without any luck.
ANY help is appreciated.
Thank you.
cheers.