Skip to main content
 首页 » 编程设计

c#之基本的 C# 文本冒险被打破

2024年02月05日16zhengyun_ustc

当我按下一个键时,它会为我提供与该键相关的所有文本,而不仅仅是第一个 WriteLine。

例如,当我在第一个屏幕上按 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(); 
    } 
} 

我做错了什么?

请您参考如下方法:

您需要一个 key = Console.ReadKey(true); 就在每个 决策点之前。例如

Console.WriteLine("Press 'Q' to go left or 'E' to go right"); 
key = Console.ReadKey(true);