循环整个consoleApp

时间:2015-06-29 22:10:31

标签: c# loops math console

嘿,我有这个数学控制台应用程序,我想循环。 它用瑞典语写成,但基本就是在" Console.ReadKey();"之后。作为暂停,应用程序应从头开始。就像使用"启动"使用.bat文件一样。和"转到:开始"。

所以基本上我想重新启动应用程序,如果通过清除屏幕然后再次应用代码或关闭并重新打开CMD窗口来完成它是很重要的。

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

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();
    }

1 个答案:

答案 0 :(得分:2)

只需绕过您的应用程序一段时间,并询问用户他/她是否想要在循环结束时退出:

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;
    }
}