【问题标题】:While Loops and If statements for my code我的代码的 While 循环和 If 语句
【发布时间】:2014-01-16 15:59:02
【问题描述】:

我正在编写一个程序,在其中你遇到一个怪物,然后让你选择是攻击还是防御。攻击代码时看起来不错,但是当我进入 Defend 时,程序就会关闭。并且当玩家HP或怪物HP

http://pastebin.com/kU8jtQwb

    public string playerChoice()
    {
        Console.WriteLine("Do you want to Attack or Defend?");
        string playerchoice = Console.ReadLine();
        return playerchoice;
    }

    public void MonsterEncouter()
    {
        Random random = new Random();
        int randomNumber = random.Next(1, 101);
        int playerHealth = random.Next(50, 101);
        int playerAttack = random.Next(10, 31);
        string userInput = "";



        if (randomNumber >= 1 && randomNumber <= 24)
        {
            Console.WriteLine("You've run into a goblin, an easy enemy");
            Console.WriteLine("Its HP is: " + randomNumber);
            Console.WriteLine("Your HP is: " + playerHealth);
            Console.WriteLine("Let the battle begin!");
            Console.ReadLine();
        }
        else if (randomNumber >= 25 && randomNumber <= 49)
        {
            Console.WriteLine("You've run into a dire wolf, a bit tough enemy");
            Console.WriteLine("It's HP is: " + randomNumber);
            Console.WriteLine("Your HP is: " + playerHealth);
            Console.WriteLine("Let the battle begin!");
            Console.ReadLine();
        }
        else if (randomNumber >= 50 && randomNumber <= 74)
        {
            Console.WriteLine("You've run into an orc, a hard enemy");
            Console.WriteLine("It's HP is: " + randomNumber);
            Console.WriteLine("Your HP is: " + playerHealth);
            Console.WriteLine("Let the battle begin!");
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("You've run into a giant, a very hard enemy");
            Console.WriteLine("It's HP is: " + randomNumber);
            Console.WriteLine("Your HP is: " + playerHealth);
            Console.WriteLine("Let the battle begin!");
            Console.ReadLine();
        }

        {
            userInput = playerChoice();
            if (userInput == "Attack")
            {
                while (userInput == "Attack" && playerHealth > 0 && randomNumber > 0)
                {
                    Console.WriteLine("You attacked the beast!");
                    Console.WriteLine("You've dealt " + playerAttack + " damage");
                    randomNumber -= playerAttack;
                    Console.WriteLine("It's HP is: " + randomNumber);
                    Console.WriteLine();
                    {
                        Console.ReadLine();
                        Console.WriteLine("It fought back! It dealt " + playerAttack);
                        playerHealth -= playerAttack;
                        Console.WriteLine("You have " + playerHealth + " HP left");
                        Console.WriteLine();
                        userInput = playerChoice();
                    }
                }
                if (randomNumber <= 0)
                {
                    Console.WriteLine();
                    Console.WriteLine("The monster has been slain!");
                    Console.WriteLine("Congratulations!");
                    Console.ReadLine();
                }
                if (playerHealth <= 0)
                {
                    Console.WriteLine();
                    Console.WriteLine("You have been slain!");
                    Console.WriteLine("You lose");
                    Console.ReadLine();
                }
            }
            else if (userInput == "Defend")
            {
                while (userInput == "Defend" && playerHealth > 0 && randomNumber > 0)
                {
                    Console.WriteLine("You are ready to defend!");
                    Console.WriteLine("The monster attacked!");
                    playerHealth -= 1;
                    Console.WriteLine("You blocked it's attacked and only lost 1 HP");
                    Console.WriteLine("You now have: " + playerHealth + " HP");
                    Console.WriteLine();
                    userInput = playerChoice();
                    Console.ReadLine();
                }
            }
        }

【问题讨论】:

  • 您要解决的问题是什么?如果您没有具体识别它,您可能会得到无用且不相关的建议样式更改的答案(如下所示)。

标签: c# loops while-loop


【解决方案1】:

您需要某种“外循环”。你的程序结构是这样的

userInput = playerChoice();
if (userInput == "Attack") {
   while (userInput == "Attack") {
      ....
      userInput = playerChoice();
   }
} else if (userInput == "Defend") {
   while (userInput == "Defend") {
      ....
      userInput = playerChoice();
   }
}

当您第一次输入“攻击”时,程序会进入第一个if 分支并停留在那里,直到您停止订购更多攻击。然后它离开if 构造并终止。

我想你想要类似的东西

userInput = playerChoice();
while (userInput == "Attack" || userInput == "Defend") {
    if (userInput == "Attack") {
       while (userInput == "Attack") {
          ....
          userInput = playerChoice();
       }
    } else if (userInput == "Defend") {
       while (userInput == "Defend") {
          ....
          userInput = playerChoice();
       }
    }
}

这样在第一个“攻击”之后,您可以按任意顺序输入更多“攻击”或“防御”。

【讨论】:

  • 不知道为什么while 在if 中循环是有意义的。
【解决方案2】:

这里有几个问题。

去掉这里的大括号:

Console.WriteLine();
{
   Console.ReadLine();
   Console.WriteLine("It fought back! It dealt " + playerAttack);
   playerHealth -= playerAttack;
   Console.WriteLine("You have " + playerHealth + " HP left");
   Console.WriteLine();
   userInput = playerChoice();
}

我假设你有那些空的Console.WriteLine(); 调用,所以你的输出中有一个空行。相反,我建议重构您以前的行,以包含一个额外的换行符,例如:

Console.Write("You have " + playerHealth + " HP left\n\n");

如果您不想重组您的程序流程,以解决在玩家或怪物没有剩余 HP 后提示您的问题,请在后续调用 playerChoice() 周围添加此 if 语句:

if (playerHealth > 0 && randomNumber > 0)
{
     userInput = playerChoice();
}

我还建议将 randomNumber 重命名为更有意义的名称,例如 enemyHP。

关于“Defend”的问题,您是否调试过代码以查看该代码分支是否受到攻击?

【讨论】:

    猜你喜欢
    • 2013-06-09
    • 2016-01-12
    • 2012-06-18
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多