This is the code I have come up with. However, I wanted to be able to output either:
- (value) is in slot x.
- (value) is in slot x.
two outputs if the (value) is available in two slots -like 7.
or
- (num) is not in the array.
But not both. Can anyone help please?
public static void main(String[] args) {
int search, counter;
int num[]={3, 4, 5, 6, 7, 8, 10, 7, 9, 13};
System.out.print("Array: ");
for (int count=0; count<num.length; count++)
System.out.print(+num[count] + " ");
Scanner in = new Scanner (System.in);
System.out.print("\nValue to find: ");
search = in.nextInt();
for (counter = 0; counter < num.length; counter++ ){
if (num[counter] == search)
{
System.out.println(search + " is in slot " + (counter + 1) + ".");
}
}
if (counter == num.length)
{
System.out.println(search + " is not in the array.");
}
}
}