If Else语句逻辑

本文关键字:语句 Else If | 更新日期: 2023-09-27 18:12:10

我正在做一个基于文本的小游戏,我的一个if/else语句没有给我预期的结果。当用户输掉游戏时,它会立即跳转到"Game Over!"它应该说monsterName +"已经打败你了!"然后跳转到游戏结束。

你们能帮我指出我的逻辑错误吗?

// Main Game Loop
while (playerHealth > 0 && monsterHealth > 0)
{
    bool playerAct = askAction();
    if (playerAct == true)// User attacks
    {
        monsterHealth = monsterHealth - playerAtk;
        if (monsterHealth < 0)
        {
            Console.WriteLine(monsterName + " falls to the ground, defeated. Congradulation, " + name + "!");
        }
        else
        {
            Console.WriteLine(monsterName + " takes a mighty swing back at you!");
            playerHealth = playerHealth - monsterAtk;
        }
    }                
    else// User defends
    {
        playerHealth = playerHealth - defendAtk;
        if (playerHealth < 0)
        {
            Console.WriteLine(monsterName + " has defeated you!");
        }
        else
        {
            Console.WriteLine(monsterName + " swings at you. It glances off your defenses.");
        }
    }               
}
// End Game
Console.WriteLine("Game Over!");
Console.ReadLine();            

我想那是相关的代码,但为了以防万一,这里是整个程序。

namespace textBasedGame
{
    class Program
    {
        static void Main(string[] args)
        {
        //Variables
        string name;
        string monsterName;
        int playerAtk = 5;
        int monsterAtk = 2;
        int defendAtk = 1;
        int playerHealth = 2;
        int monsterHealth = 3;
        // Gathers information from the user
        Console.WriteLine("What is your name?");
        name = Console.ReadLine();
        Console.WriteLine("What is your enemies name?");
        monsterName = Console.ReadLine();
        //Story Intro
        Console.WriteLine("You are hunting in the swamp and in the darkness of the overgrowth you see a movement.");
        Console.WriteLine("You creep closer to the silouette.");
        Console.WriteLine("A little closer....");
        Console.WriteLine("It looks familiar....");
        Console.WriteLine("It is " + monsterName + "!!!");
        Console.WriteLine("Now is your chance!");
        // Main Game Loop
        while (playerHealth > 0 && monsterHealth > 0)
        {
            bool playerAct = askAction();
            if (playerAct == true)// User attacks
            {
                monsterHealth = monsterHealth - playerAtk;
                if (monsterHealth < 0)
                {
                    Console.WriteLine(monsterName + " falls to the ground, defeated. Congradulation, " + name + "!");
                }
                else
                {
                    Console.WriteLine(monsterName + " takes a mighty swing back at you!");
                    playerHealth = playerHealth - monsterAtk;
                }
            }                
            else// User defends
            {
                playerHealth = playerHealth - defendAtk;
                if (playerHealth < 0)
                {
                    Console.WriteLine(monsterName + " has defeated you!");
                }
                else
                {
                    Console.WriteLine(monsterName + " swings at you. It glances off your defenses.");
                }
            }               
        }
        // End Game
        Console.WriteLine("Game Over!");
        Console.ReadLine();            
    }
    static bool askAction()//Displays HP and asks for user action. Returns true or false
    {
        bool move;
        Console.WriteLine("What would you like to do?(type: attack or defend)");
        string playerAction = Console.ReadLine();
        if (playerAction == "attack")
        {
            return move = true;
        }
        else
        {
            return move = false;
        }
    }
}
}

If Else语句逻辑

while (playerHealth > 0 && monsterHealth > 0)
if (playerHealth < 0)

只要玩家的生命值高于零,while循环就会继续。if语句在它们的健康值低于0时触发。如果它们的运行状况恰好为零会发生什么?

首先,我纠正了这个函数…基本上你不需要布尔值"move",因为你没有在函数中使用它,所以你没有为任何东西创建它。

当你的函数是"bool"类型时,它返回一个"bool"类型。换句话说,你可以做

return true;

return false;

不需要

return move = true;

试试这个:

static bool askAction()//Displays HP and asks for user action. Returns true or false
{
    Console.WriteLine("What would you like to do?(type: attack or defend)");
    string playerAction = Console.ReadLine();
    if (playerAction == "attack")
    {
        return true;
    }
    return false;
}

这是你的主循环。我猜你的怪物死于0生命值,所以我改变了符号

while (playerHealth > 0 && monsterHealth > 0)
{
    bool playerAct = askAction();
    if (playerAct == true)// User attacks
    {
        monsterHealth = monsterHealth - playerAtk;
        if (monsterHealth <= 0)
        {
            Console.WriteLine(monsterName + " falls to the ground, defeated. Congradulation, " + name + "!");
        }
        else
        {
            Console.WriteLine(monsterName + " takes a mighty swing back at you!");
            playerHealth = playerHealth - monsterAtk;
        }
    }                
    else// User defends
    {
        playerHealth = playerHealth - defendAtk;
        if (playerHealth <= 0)
        {
            Console.WriteLine(monsterName + " has defeated you!");
        }
        else
        {
            Console.WriteLine(monsterName + " swings at you. It glances off your defenses.");
        }
    }               
}

哦,顺便说一下,在节目结束时,你说"游戏结束",但如果玩家赢了呢?

我看到的唯一逻辑错误是,你有monsterHealth = 2playerAtk = 5,所以怪物在每次第一次攻击中都会死亡,monsterAtk = 2;playerHealth=2也是如此。同样,玩家在第一次攻击时死亡。所以这就变成了一招一棋。

对于防御,当玩家的生命值为0时,它将不需要打印。