2013-10-31 43 views
0

我不知道爲什麼在我添加最後的if聲明後,我得到了這個錯誤 我的代碼工作的很好,但添加if我得到這個錯誤。我真的很感激,如果你能幫助我解決這個問題。Invalid token'('in class,struct或interface member declaration if after

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(); 
+1

呃,請格式化您的代碼。大多數編輯甚至可以自動爲你做。 – kviiri

+0

'int dragonHealth = 150;'之前還有一個額外的緊密支撐; – siride

回答

0

你的代碼僅僅是局部的,所以我不能100%肯定,但我認爲錯誤是造成錯配或錯位括號{}。你幾乎要求這樣那樣的錯誤,當你不縮進代碼。正確格式化您的代碼,並仔細檢查你的括號。

似乎奇怪對我特別的地方:

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? 

作爲一個側面說明無關你的實際問題,學習一些面向對象編程或最起碼的使用方法會幫助你做出了較大的文字冒險遊戲用更少的努力!