循环整个控制台 [英] Loop the whole consoleApp

查看:57
本文介绍了循环整个控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有想要循环的数学控制台应用程序.它是用瑞典语编写的,但基本的是在"Console.ReadKey();"之后.这是一个暂停,应用程序应从头开始.就像您可以使用开始"和转到:开始"来处理.bat文件一样.

Hey i have this math console application that i would like to loop. Its written in Swedish, but the basic just is that after the "Console.ReadKey();" which acts as a pause the application should start over from the beginning. Much like you can do with .bat files using "start" and "goto: start".

所以基本上我想重新启动该应用程序,这与通过清除屏幕然后重新应用代码或关闭并重新打开CMD窗口来完成无关紧要.

So basically i would like to start the app all over again, it dosnt matter if its done by clearing the screen and then applying the code all over again or by closing and reopening the CMD window.

谨向奥斯卡·安德森(Oscar Andersson)瑞典17岁,理工科学生

Great regards Oscar Andersson 17 Sweden, tech student.

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

推荐答案

只需在应用程序中绕一会儿,然后在循环结束时询问用户是否要退出:

Just wrap a while around your application and ask the user if he/she wants to quit when at end of the loop:

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

这篇关于循环整个控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆