當我按下一個鍵時,它會給我所有與該鍵有關的文本,而不僅僅是第一個WriteLine
。基本C#文本冒險破解
例如,當我按下Q
甚至在第一個屏幕上,它說
"the door opens and you are blinded by the sun"
"you walk to the town and are taken back to your cell"
而不是僅僅把我送到下一個決策點。
class Program
{
static void Main(string[] args)
{
//Int
//String
Console.WriteLine("you find yourself in a dark room");
Thread.Sleep(1000);
Console.WriteLine("In front of you is a door");
Thread.Sleep(1000);
Console.WriteLine("Press 'W' to go through");
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.W)
{
Console.WriteLine("You enter a room with two doors on either side");
Thread.Sleep(1000);
}
Console.WriteLine("Press 'Q' to go left or 'E' to go right");
{
if (key.Key == ConsoleKey.E)
{
Console.WriteLine("You enter the room to the right and are eaten by a grue");
}
else if (key.Key == ConsoleKey.Q)
{
Console.WriteLine("The door opens and you are blinded by the sun");
Thread.Sleep(1000);
Console.WriteLine("You walk down the road and come to a fork in it");
}
Thread.Sleep(1000);
Console.WriteLine("Press 'Q' to go left, 'E' to go right, or 'W' to pick up the fork!");
if (key.Key == ConsoleKey.Q)
{
Console.WriteLine("You walk to a town and are taken back to your cell by the guards");
}
}
if (key.Key == ConsoleKey.E)
{
Console.WriteLine("You escape into the mountains! you win! ");
}
else if (key.Key == ConsoleKey.W)
{
Console.WriteLine("you pick up the fork unravelling space and time. You monster.");
}
Console.ReadLine();
}
}
我在做什麼錯在這裏?
那麼你只調用'ReadKey'一次...雖然你正在測試'W'被按下,如果第一個鍵*不是''W',你只是不顯示「兩扇門的房間」......你想在那裏發生什麼?你想循環,直到用戶*按下'W'?它不會幫助你的代碼沒有很好的格式化,並且你有一個沒有明顯原因的嵌套塊(在第一個「按Q向左走等」之後) –
_巨大的代碼牆會造成10點傷害!_ – MickyD
此代碼中的縮進是不可思議的:-) 不知道它是否因爲您沒有嘗試使它更清潔 –