0

我不知道为什么在我添加最后一条if语句后,我得到了这个错误。我的代码运行良好,但在添加后if我得到了这个错误。如果您能帮我解决这个问题,我将不胜感激。

string answer = Console.ReadLine();
if (answer == "cold" || answer == "a cold")
{
    Console.WriteLine("Well done you may pass");
    Console.WriteLine("Doors are now Open");
    Console.ReadLine();
}



else if (selection == "W" ||selection == "w" && answer != "cold" || answer != "a cold") 
{
    characterHealth = characterHealth - 20;
    Console.WriteLine("Wrong Answer Try Again");
    Console.WriteLine("Your health level is: {0}", characterHealth);
    Console.ReadLine();
}
else if (selection == "S" || selection == "s" && answer != "cold" || answer != "a cold")
{
    characterHealth = characterHealth - 10;
    Console.WriteLine("Wrong Answer Try Again");
    Console.WriteLine("Your health level is: {0}", characterHealth);
    Console.ReadLine();
}

}
int dragonHealth = 150;
int strike = randomNumbers.Next(1, 30);
int strike1 = randomNumbers.Next(1, 50);
Console.Writeline("As you move on you are encountering a small dragon");
while (dragonHealth >= 0 && characterHealth >= 0)
{
    dragonHealth = dragonHealth - strike1;
    characterHealth = characterHealth - strike; 
    Console.WriteLine("You strike the dragon,  its health is: {0}", dragonHealth);
    Console.WriteLine("The dragon counters,  your health is: {0}", characterHealth);

    if ( characterHealth >= 0 && dragonHealth <= 0)
    {
        Console.WriteLine("Congratulations you have killed the dragon");
        Console.WriteLine("You found the tresure, Story ends here");
        Console.ReadLine();
    }
    if ( characterHealth <= 0 && dragonHealth >=0)
    {
        Console.WriteLine("You died Game over");
        return;
    }
    if ( characterHealth <= 0 && dragonHealth <=0)
    {
        Console.WriteLine("You both died at the same time bad luck");
        return;
    }
    Console.ReadLine();
4

1 回答 1

0

您的代码只是部分代码,因此我不能 100% 确定,但我相信错误是由 mismatched or misplaced brackets 引起的{}。当您不缩进代码时,您实际上是在要求这种错误。正确格式化您的代码并仔细检查您的大括号。

对我来说似乎很特别的地方:

 else if (selection == "S" || selection == "s" && answer != "cold" || answer != "a cold")
        {
        characterHealth = characterHealth - 10;
        Console.WriteLine("Wrong Answer Try Again");
        Console.WriteLine("Your health level is: {0}", characterHealth);
        Console.ReadLine();
        }

 } //what does this brace close?
...
while (dragonHealth >= 0 && characterHealth >= 0)
    { //where is this brace closed?

作为与您的实际问题无关的旁注,学习一些面向对象的编程或至少使用方法将帮助您以更少的努力制作更大的文本冒险游戏!

于 2013-10-31T22:33:05.010 回答