如何在我制作的游戏中添加计数系统

How can I add a counting system to my game I made

我是初学者,我在 C# 控制台中编写代码。我制作了这个猜谜游戏,您必须在其中猜测 1 到 10 之间的随机数。我制作这个游戏是为了让您可以无限次玩它。游戏按计划完美运行,但我想要在这款游戏中添加更多内容,即计数系统。我希望我的代码能够计算玩家为获得正确猜测而进行的尝试。试了好几种方法都不行。

using System;

namespace The_Test_Program
{
    class Program
    {
        static void Main(string[] args)
        {
            // Appearance
            Console.Title = "Test";
    
    // Working Code
    Console.WriteLine("I will generate a random number and you have to guess it.");
        Random random = new Random();
        int randomNum = random.Next(1,11);
        
        Console.WriteLine("I have generated a random number from 1 to 10.");
        Console.Write("What do you think write the answer : ");
        int GenNum = int.Parse(Console.ReadLine());
        Char yN;

        while (GenNum!=randomNum)
        {
            Console.Clear();
            Console.WriteLine("\nWrong");  
            Console.Write("Wanna try again [Y/N]: ");  
            yN = Char.Parse(Console.ReadLine());
            if (yN =='Y')
            {                    
                randomNum = random.Next(1,11);
                Console.WriteLine("I have generated a new random number.");
                Console.Write("Try again : ");
                GenNum = int.Parse(Console.ReadLine());
                
            }
            else
            {
                Console.WriteLine("No problem");
                Console.WriteLine("\nPress any key to exit .....");
                Console.ReadKey();
                Environment.Exit(0);
            }
        }    
        
        Console.WriteLine("Correct");
    }
}

}

首先欢迎来到 stack overflow,希望您浏览愉快:)

我建议进行一些更改,您可以将 while 检查更改为:

while (GenNum!=randomNum && yN != 'N')

这将允许您稍后提取 if 逻辑,并在这两个语句正确时循环。

如果要统计循环次数(统计玩家玩了多少次),可以在开头加一个int counter,然后在while中加counter++环形。 ++ 与 counter = counter + 1 相同,但方式更奇特。

下面的代码有这些要点,还有一些您可能想要更改的地方,但这是您明白要点的地方!

using System;

namespace The_Test_Program
{
    class Program
    {
        static void Main()
        {
            // Appearance
            Console.Title = "Test";
    
            // Working Code
            Console.WriteLine("I will generate a random number and you have to guess it.");
            Random random = new Random();
            int randomNum = random.Next(1,11);
            
            Console.WriteLine("I have generated a random number from 1 to 10.");
            Console.Write("What do you think write the answer : ");
            int GenNum = int.Parse(Console.ReadLine());
            Char yN = 'Y';
            int counter = 0;
    
            while (GenNum!=randomNum && yN != 'N')
            {
                Console.Clear();
                Console.WriteLine("\nWrong");
                Console.Write("Wanna try again [Y/N]: ");  
                yN = Char.Parse(Console.ReadLine());
                counter++;
                if (GenNum == randomNum)
                {
                    Console.WriteLine($"Correct, you tried {counter} times.");
                }
            }
            Console.WriteLine("No problem");
            Console.WriteLine("\nPress any key to exit .....");
            Console.ReadKey();
            Environment.Exit(0);
        }
    }
}