这是我想出的代码。然而,我希望能够输出:数组值超过一个输出
- (值)在槽x中。
- (值)在时隙x中。
两个输出,如果(值)在两个时隙中可用样7.
或
- (NUM)不是在数组中。
但不是两者都有。 任何人都可以帮忙吗?
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.");
}
}
}
假设使用布尔标志最合适的解决办法。 –