循环整个控制台应用程序

本文关键字:应用程序 控制台 循环 | 更新日期: 2023-09-27 18:00:20

嘿,我有一个数学控制台应用程序,我想循环它。它是用瑞典语写的,但基本的是,在"Console.ReadKey();"之后,应用程序应该从头开始。就像您可以使用"start"answers"goto:start"处理.bat文件一样。

因此,基本上我想重新启动应用程序,无论是通过清除屏幕,然后重新应用代码,还是通过关闭并重新打开CMD窗口,都无关紧要。

非常感谢17岁的瑞典科技学生Oscar Andersson。

static void Main(string[] args)
    {
        //Förberedelser
        Random numberGenerator = new Random();
        int num01 = numberGenerator.Next(1,11);
        int num02 = numberGenerator.Next(1,11);
        //Frågan
        Console.ForegroundColor = ConsoleColor.White;
        Console.Write("Vad är ");
        Console.ForegroundColor = ConsoleColor.Green;
        Console.Write(num01 + " ");
        Console.ForegroundColor = ConsoleColor.White;
        Console.Write("gånger ");
        Console.ForegroundColor = ConsoleColor.Green;
        Console.Write(num02);
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine(" ?");
        //Svaret
        Console.ForegroundColor = ConsoleColor.Magenta;
        int numKey = Convert.ToInt32 (Console.ReadLine());
        if (numKey == num01 * num02)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("'n Grattis du svarade rätt!");
        }
        else
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("'n Du svarade tyvärr fel. 'n Svaret är ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(num01 * num02);
        }
        //Avslut och loop
        Console.ReadKey();
    }

循环整个控制台应用程序

只需在应用程序周围停留一段时间,并在循环结束时询问用户是否要退出:

static void Main(string[] args)
{
    bool continueApplication = true;
    while(continueApplication) {
    //Förberedelser
    Random numberGenerator = new Random();
    int num01 = numberGenerator.Next(1,11);
    int num02 = numberGenerator.Next(1,11);
    //Frågan
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write("Vad är ");
    Console.ForegroundColor = ConsoleColor.Green;
    Console.Write(num01 + " ");
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write("gånger ");
    Console.ForegroundColor = ConsoleColor.Green;
    Console.Write(num02);
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine(" ?");
    //Svaret
    Console.ForegroundColor = ConsoleColor.Magenta;
    int numKey = Convert.ToInt32 (Console.ReadLine());
    if (numKey == num01 * num02)
    {
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("'n Grattis du svarade rätt!");
    }
    else
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("'n Du svarade tyvärr fel. 'n Svaret är ");
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(num01 * num02);
    }
    Console.WriteLine("Do you want to continue(y/n)?");
    //Read what the user typed
    string result = Console.ReadLine();
    //Will allow N as well
    result = result.ToLower();
    //Check if the user typed n
    if(result == "n") {
        continueApplication = false;
    }
}