これはおそらく本当にばかげていることはわかっていますが、この if ステートメントが失敗する理由がわかりません。
1
コンソールにorを入力する2
と、最初の if ステートメントは失敗しますが、最初に bool に結果を格納すると、2 番目のステートメントは成功します。なんで?私はおそらくここでばかげたことをしていますか?
Console.WriteLine("Enter 1 for AM or 2 for PM?");
string amOrPM = Console.ReadLine();
//Why does this fail if I enter 1 or 2?
if (amOrPM != "1" || amOrPM != "2")
Console.WriteLine("You must enter 1 for AM or 2 for PM. Try again.");
//This works!
bool valid = (amOrPM != "1" || amOrPM != "2");
if (!valid)
Console.WriteLine("You must enter 1 for AM or 2 for PM. Try again.");
amOrPm
最初の if ステートメントで、|| の代わりに&&を配置1
するamOrPM
必要があることに気付きましたが、これは次のように読むため混乱し2
ます。私はこの間違った定義を読んでいますか?