if (line.contains("the") || line.contains("and") || line.contains("hello")) {
System.out.println(line + " contains the, and or hello");
} else {
System.out.println(line + " does not contain the, and or hello");
}
しかし、あなたは「こんにちは」のように追加した場合、あなたの現在のアプローチは動作するはずです(と、それは他の後
{
、ない
(
です)。
if (line.indexOf("the") >= 0 || line.indexOf("and") >= 0
|| line.indexOf("hello") >= 0) {
System.out.println(line + " contains the, and or hello");
} else {
System.out.println(line + " does not contain the, and or hello");
}
ただし、例では中カッコを省略しています。あなたはそれを行うことができますが、あなたのステートメントは次の行にのみ適用されます。
if (line.indexOf("the") >= 0 || line.indexOf("and") >= 0
|| line.indexOf("hello") >= 0)
System.out.println(line + " contains the, and or hello");
else
System.out.println(line + " does not contain the, and or hello");
そして、あなたは
if (line.indexOf("the") >= 0 || line.indexOf("and") >= 0
|| line.indexOf("hello") >= 0) {
System.out.println(line + " contains the, and or hello");
} else
System.out.println(line + " does not contain the, and or hello");
それとも
if (line.indexOf("the") >= 0 || line.indexOf("and") >= 0
|| line.indexOf("hello") >= 0)
System.out.println(line + " contains the, and or hello");
else {
System.out.println(line + " does not contain the, and or hello");
}
のように中括弧を混在させることができます。しかし、私は常にに使用ブレースを好む。
途中で文が途切れてしまった場合は、非常に難しいです... –
どこから行くのかわかりませんでした。 – YawdMan
'と'が 'バンド'という単語を見つけるのはいいですか?はいの場合、あなたは正しい道を歩いています。いいえの場合は、 'split()'や正規表現を使う必要があります。 – Andreas