これは私が思い付いたコードです。しかし、私は次のいずれかを出力できるようにしたかった:配列値複数出力
- (値)はスロット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.");
}
}
}
は、周りの最も適した仕事をブール値のフラグを使用しているとします。 –