這是我想出的代碼。然而,我希望能夠輸出:數組值超過一個輸出
- (值)在槽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.");
}
}
}
假設使用布爾標誌最合適的解決辦法。 –