而我的代码的循环和If语句

时间:2013-12-29 17:39:09

标签: c# loops while-loop

我正在写一个程序,你遇到一个怪物,然后给出你是否想要攻击或防御的选项。攻击代码似乎很好,但是当我进入Defend时,程序就会关闭。此外,当玩家HP或怪物HP <= 0时,它将再次询问用户他们是否想要攻击或防御然后显示获胜或失败的消息。我想要它,一旦它达到&lt; = 0,它只会显示消息。

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();
                }
            }
        }

2 个答案:

答案 0 :(得分:0)

你需要某种“外环”。你的程序结构就像

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();
       }
    }
}

这样在第一次“攻击”之后你可以按任何顺序输入更多“攻击”或“防守”。

答案 1 :(得分:0)

这里有几个问题。

在这里摆脱括号:

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”的问题,您是否已调试代码以查看该代码分支是否被命中?