C# Or 运算符问题

Cer*_*xes 0 c# if-statement operator-keyword

我很确定你们中有人可以帮助我解决这个问题。我没有找到也不理解解决方案。

if (hero.IsAlive || creature.IsAlive == false)
        {
            if (hero.IsAlive == true)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                EnemyHitInterface();
                Console.ForegroundColor = ConsoleColor.White;
                Thread.Sleep(1500);
                hero.Exp += 150;
                baseSetting.YouWin();
                hero.UpdateLvL();
                hero.CharacterInterface(hero);
                hero.PrintStatsHero();
                Console.ReadKey();
                CheckForItem();
                hero.PrintStatsHero();
                Console.ReadKey();
            }
            else if (creature.IsAlive == true)
            {
                Console.Clear();
                CenterText("You are dead. See you next time...", 15);
                Console.ReadKey();
                Environment.Exit(0);
            }
        }
Run Code Online (Sandbox Code Playgroud)

在我看来,如果操作很简单,一旦英雄或生物死亡,它就会被触发。如果生物死了,一切都很好,但如果英雄死了,就不会被触发。我认为什么是我不明白的?

对我来说就像:

当英雄或生物死亡时,进入 如果英雄还活着,则执行此操作 如果生物还活着,则执行此操作

但就像我说的,对于生物来说这是行不通的

Jam*_*iec 7

我怀疑你读过这行

if (hero.IsAlive || creature.IsAlive == false)
Run Code Online (Sandbox Code Playgroud)

在你的脑海中“如果英雄或生物死了”。它实际上说的是“如果英雄还活着或者生物死了”。如果你遵循其余的逻辑,你就会明白为什么你从来没有击中这个else if (creature.IsAlive == true)部分。

你(我认为)在第一个 if 语句中想要的是

if (hero.IsAlive == false || creature.IsAlive == false) // if either are dead
Run Code Online (Sandbox Code Playgroud)

或者,更通常写成

if (!hero.IsAlive || !creature.IsAlive) // if either are dead
Run Code Online (Sandbox Code Playgroud)