【问题标题】:C# Text-Based Game reads as no errors but closes after first choiceC# Text-Based Game 读取为没有错误但在第一次选择后关闭
【发布时间】:2019-02-26 19:35:12
【问题描述】:

这是示例,请放轻松,因为这是我真正编写的第一个代码。我不确定格式是否会完全翻译。这段代码运行良好,直到最后一次编辑我将“rand”更改为“rand1”,就好像给了我一个“rand”已被使用的错误。现在,当我运行它时,我会“战斗或隐藏”,一旦我输入其中一个选项,终端就会关闭。然而,Visual Studio 没有显示任何错误。

namespace Text_Game
{
class Game
{
    static int playerHealth = 125;
    static int playerAttack = 10;
    static string playerName;
    static string playerChoice;

    static int enemyHealth = 100;
    static int enemyAttack = 5;


    static void Main(string[] args)
    {
        Welcome();
        FightLoop();
    }

    private static void Welcome()
    {
        Console.WriteLine("Hello Traveller, What is your name?");
        playerName = Console.ReadLine();

        Console.WriteLine("Well Hello, " + playerName);
        Console.WriteLine("You are alone in a dark dungeon. You can see a room behind you and a long hall in front of you.");
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("You hear an enemy nearby...");
        Console.ForegroundColor = ConsoleColor.DarkCyan;
        Console.WriteLine("(fight) or (hide)?");
        Console.ForegroundColor = ConsoleColor.Gray;
        Console.ReadLine();
    }




    private static void FightLoop()
    {
        if (playerChoice == "fight")
        { 
            do
            {
               Console.WriteLine("You have " + playerHealth + "health left");
               Console.WriteLine("Enemy has " + enemyHealth + "health left");
               Console.ForegroundColor = ConsoleColor.DarkCyan;
               Console.WriteLine("(attack) or (defend)?");
               Console.ForegroundColor = ConsoleColor.Gray;
               playerChoice = Console.ReadLine(); Console.WriteLine();

                if (playerChoice == "attack")
                {
                  Random rand = new Random();
                  int attackdamage = (rand.Next(3, 5) * playerAttack);
                  enemyHealth = enemyHealth - attackdamage;

                  Console.ForegroundColor = ConsoleColor.Red;
                  Console.WriteLine("Your attack did {0} damage!", attackdamage);
                  Console.ForegroundColor = ConsoleColor.Gray;

                    //enemy attacks back
                  Console.ForegroundColor = ConsoleColor.Red;
                  Console.WriteLine("The enemy attacked you!");
                  Console.ForegroundColor = ConsoleColor.Gray;

                  Random rand1 = new Random();
                  int enemydamage = (rand.Next(2, 4) * enemyAttack);
                  playerHealth = playerHealth - enemydamage;

                  Console.ForegroundColor = ConsoleColor.Red;
                  Console.WriteLine("The enemy's attack did {0} damage!", enemydamage);
                  Console.ForegroundColor = ConsoleColor.Gray;


                }
                else if (playerChoice == "defend")
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("The enemy attacked you!");
                    Console.ForegroundColor = ConsoleColor.Gray;

                    Random rand = new Random();
                    int enemydamage = (rand.Next(2, 4) * enemyAttack);
                    playerHealth = playerHealth - enemydamage/2;

                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("The enemy's attack did {0} damage!", enemydamage/2);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                else
                {
                    Console.WriteLine("Please choose (attack) or (defend).");
                }

            } while (playerHealth > 0 && enemyHealth > 0);


            if (enemyHealth <= 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("You have defeated the enemy!");
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("You were able to escape!");
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Thanks for playing!");
            }
            if (playerHealth <= 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("The enemy has killed you!");
                Console.ForegroundColor = ConsoleColor.Gray;
            }


        }
        else if (playerChoice == "hide")
        {
            Console.WriteLine("You hid in the room behind you, but the enemy heard and is coming!");
            Console.WriteLine("You can see a wardrobe in the corner.");
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.WriteLine("(wardrobe) or (stay)?");
            Console.ForegroundColor = ConsoleColor.Gray;

            if (playerChoice == "wardrobe")
            {
                Console.WriteLine("You are now hidden.");
                Console.WriteLine("The enemy leaves through a secret door. You follow and find yourself outside.");
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("You have escaped!");
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Thanks for playing!");

            }
            else if (playerChoice == "stay")
            {
                do
                {
                    Console.WriteLine("You have " + playerHealth + "health left");
                    Console.WriteLine("Enemy has " + enemyHealth + "health left");
                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                    Console.WriteLine("(attack) or (defend)?");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    playerChoice = Console.ReadLine(); Console.WriteLine();

                    if (playerChoice == "attack")
                    {
                        Random rand = new Random();
                        int attackdamage = (rand.Next(3, 5) * playerAttack);
                        enemyHealth = enemyHealth - attackdamage;

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Your attack did {0} damage!", attackdamage);
                        Console.ForegroundColor = ConsoleColor.Gray;

                        //enemy attacks back
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("The enemy attacked you!");
                        Console.ForegroundColor = ConsoleColor.Gray;

                        Random rand1 = new Random();
                        int enemydamage = (rand.Next(2, 4) * enemyAttack);
                        playerHealth = playerHealth - enemydamage;

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("The enemy's attack did {0} damage!", enemydamage);
                        Console.ForegroundColor = ConsoleColor.Gray;


                    }
                    else if (playerChoice == "defend")
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("The enemy attacked you!");
                        Console.ForegroundColor = ConsoleColor.Gray;

                        Random rand = new Random();
                        int enemydamage = (rand.Next(2, 4) * enemyAttack);
                        playerHealth = playerHealth - enemydamage / 2;

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("The enemy's attack did {0} damage!", enemydamage / 2);
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    else
                    {
                        Console.WriteLine("Please choose (attack) or (defend).");
                    }

                } while (playerHealth > 0 && enemyHealth > 0);



                if (enemyHealth <= 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("You have defeated the enemy!");
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.WriteLine("You have escaped!");
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine("Thanks for playing!");
                }
                if (playerHealth <= 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("The enemy has killed you!");
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
            }

        }
        else
        {
            Console.WriteLine("Choose (fight) or (hide).");
        }

    }
}

}

【问题讨论】:

  • 当输入FightLoop 时,playerChoice 的值将为空。这将使它进入最终的else 块,该块仅包含一个Console.WriteLine。因此,没有什么可以阻止程序打印该行并退出 FightLoop 并因此完成执行。
  • 放一个 Console.ReadLine();语句,以便它等待 Enter 关闭控制台。节目结束
  • 就像旁注一样,FightLoop 方式太大了。如果你把它分解成单独的方法,以后调试就不会那么痛苦了。
  • 学习使用调试器的基础知识没有错。我会说这是一个相当好的时机。真的,一旦您了解了基本的调试技术(例如单步调试),您将不会相信您曾经尝试编写/测试/故障排除程序却无法使用调试器:Learn to debug using Visual Studio, Navigating through Code with the Debugger
  • 我没有看到你设置了变量 playerChoice。

标签: c# text-based


【解决方案1】:

FightLoop 方法开始执行时,playerChoice 变量没有值。所以执行到block 3,如下所示,然后退出方法。

private static void FightLoop()
{
    if (playerChoice == "fight")
    {
         //block 1
    }
    else if (playerChoice == "hide")
    {
         //block 2
    }
    else
    {
        //block 3
        Console.WriteLine("Choose (fight) or (hide).");
    }
}

您需要更改 Welcome 方法的最后一行,以便将读取的字符串保存到 playerChoice 变量中。

private static void Welcome()
{
    Console.WriteLine("Hello Traveller, What is your name?");
    playerName = Console.ReadLine();

    Console.WriteLine("Well Hello, " + playerName);
    Console.WriteLine("You are alone in a dark dungeon. You can see a room behind you and a long hall in front of you.");
    Console.ForegroundColor = ConsoleColor.DarkRed;
    Console.WriteLine("You hear an enemy nearby...");
    Console.ForegroundColor = ConsoleColor.DarkCyan;
    Console.WriteLine("(fight) or (hide)?");
    Console.ForegroundColor = ConsoleColor.Gray;
    playerChoice = Console.ReadLine();
}

由于您没有在任何地方使用 rand1 变量,因此可以删除以下行。

Random rand1 = new Random();

另外,一个好的做法是使用“战斗”和“隐藏”的方法:

private static void FightLoop()
{
    if (playerChoice == "fight")
    {
         fight();
    }
    else if (playerChoice == "hide")
    {
         hide();
    }
    else
    {
        Console.WriteLine("Choose (fight) or (hide).");
    }
}

【讨论】:

    【解决方案2】:

    问题出在您的Welcome() 方法上。您要求玩家选择战斗或隐藏,但您没有将答案保存在 playerChoice 变量中。因此,当您在 FightLoop 上检查 playerChoice 时,它会在 else 结束,运行 Console.WriteLine("Choose (fight) or (hide)."); 行。但是因为您没有等待任何按键,所以程序完成并关闭。要修复,您必须:

    在Welcome 方法中读取玩家选择,将Console.ReadLine(); 行更改为playerChoice = Console.ReadLine();

    等待战斗循环中else 部分的任何按键,在Console.WriteLine("Choose (fight) or (hide)."); 之后添加Console.ReadLine();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      相关资源
      最近更新 更多