C# 如何循环控制台应用程序

C# 如何循环控制台应用程序,c#,visual-studio,console,C#,Visual Studio,Console,我只需要能够循环一个控制台应用程序。我的意思是: program start: display text get input do calculation display result display text get input. REPEAT PROCESS INFINATE NUMBER OF TIMES UNTIL THE USER EXITS THE APPLICATION. program end. 我希望这是有道理的。谁能解释一下我将如何着手做这件事?谢谢:) 用户可以使用CT

我只需要能够循环一个控制台应用程序。我的意思是:

program start:
display text
get input
do calculation
display result
display text
get input.

REPEAT PROCESS INFINATE NUMBER OF TIMES UNTIL THE USER EXITS THE APPLICATION.
program end.
我希望这是有道理的。谁能解释一下我将如何着手做这件事?谢谢:)

用户可以使用
CTRL-C
在任意点停止程序


这就是你的意思吗?

你可以在你的程序中做的任何事情周围设置一个循环。

使用While循环

bool userWantsToExit = false;

get input

while(!userWantsToExit)
{

  do calc;
  display results;
  display text;
  get input;
  if (input == "exit") 
    userWantsToExit = true;
}

program end;

您可以在program.cs中将Main方法的整个主体包装在while循环中,并始终满足一个条件

例如(在伪代码中)

善良

Dan使用系统; 使用System.Collections.Generic; 使用系统诊断; 使用System.Linq; 使用系统文本; 使用系统线程; 使用System.Threading.Tasks; 名称空间输入循环 { 班级计划 { 静态长打印fpseveryxms=5000; 静态双限值fpsto=10.0; 静态void Main(字符串[]参数) { ConsoleKeyInfo Key=新的ConsoleKeyInfo(“”,ConsoleKey.Spacebar,false,false,false); 长TotalFrameCount=0; 长帧数=0; 双限帧时间=1000.0/LimitFPSTo; 做 { 秒表FPSTimer=Stopwatch.StartNew(); 而(!Console.KeyAvailable) { //滴答声开始 秒表SW=Stopwatch.StartNew(); //实际滴答声 勾选(); //滴答声结束 SW.Stop(); ++总帧数; ++帧数; 如果(FPSTimer.elapsedmillesons>printfseveryxms) { FrameCount=PrintFPS(FrameCount,FPSTimer); } if(SW.eassed.total毫秒是否要退出/重新启动程序?“你能澄清一下吗?”迈克尔·范恩格伦。我2000年高中毕业。我现在24岁了我希望回到学校,萝琳。另外,这里有一个很好的讨论,探讨更为CPU友好的方法:
bool userWantsToExit = false;

get input

while(!userWantsToExit)
{

  do calc;
  display results;
  display text;
  get input;
  if (input == "exit") 
    userWantsToExit = true;
}

program end;
Console.WriteLine("bla bla - enter xx to exit");
string line;
while((line = Console.ReadLine()) != "xx")
{
  string result = DoSomethingWithThis(line);
  Console.WriteLine(result);
}
While (true)
{
   Body
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace InputLoop
{
    class Program
    {
        static long PrintFPSEveryXMilliseconds = 5000;
        static double LimitFPSTo = 10.0;
        static void Main(string[] args)
        {
            ConsoleKeyInfo Key = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false);
            long TotalFrameCount = 0;
            long FrameCount = 0;
            double LimitFrameTime = 1000.0 / LimitFPSTo;
            do
            {
                Stopwatch FPSTimer = Stopwatch.StartNew();
                while (!Console.KeyAvailable)
                {
                    //Start of Tick
                    Stopwatch SW = Stopwatch.StartNew();

                    //The Actual Tick
                    Tick();

                    //End of Tick
                    SW.Stop();
                    ++TotalFrameCount;
                    ++FrameCount;
                    if (FPSTimer.ElapsedMilliseconds > PrintFPSEveryXMilliseconds)
                    {
                        FrameCount = PrintFPS(FrameCount, FPSTimer);
                    }
                    if (SW.Elapsed.TotalMilliseconds < LimitFrameTime)
                    {
                        Thread.Sleep(Convert.ToInt32(LimitFrameTime - SW.Elapsed.TotalMilliseconds));
                    }
                    else
                    {
                        Thread.Yield();
                    }
                }
                //Print out and reset current FPS
                FrameCount = PrintFPS(FrameCount, FPSTimer);

                //Read input
                Key = Console.ReadKey();

                //Process input
                ProcessInput(Key);
            } while (Key.Key != ConsoleKey.Escape);
        }

        private static long PrintFPS(long FrameCount, Stopwatch FPSTimer)
        {
            FPSTimer.Stop();
            Console.WriteLine("FPS: {0}", FrameCount / FPSTimer.Elapsed.TotalSeconds);
            //Reset frame count and timer
            FrameCount = 0;
            FPSTimer.Reset();
            FPSTimer.Start();
            return FrameCount;
        }

        public static void Tick()
        {
            Console.Write(".");
        }

        public static void ProcessInput(ConsoleKeyInfo Key)
        {
            Console.WriteLine("Pressed {0} Key", Key.KeyChar.ToString());
        }
    }
}