如何循环运行控制台应用程序

6

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

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.

我希望这有意义。请问有人能解释一下我该如何做吗?谢谢 :)

您想让程序退出/重新启动吗?您能澄清一下吗? - Rippo
@Michael Van Engelen。我在2000年完成了高中学业,现在已经24岁了。;) 我希望我能回到学校哈哈 - jay_t55
6个回答

19
Console.WriteLine("bla bla - enter xx to exit");
string line;
while((line = Console.ReadLine()) != "xx")
{
  string result = DoSomethingWithThis(line);
  Console.WriteLine(result);
}

8
while(true) {
  DisplayText();
  GetInput();
  DoCalculation();
  DisplayResult();
  DisplayText();
  GetInput();
}

用户可以通过按下CTRL-C键随时停止程序。
这是你的意思吗?

6
您可以在program.cs中的Main方法主体周围使用while循环,其条件始终满足。
例如(伪代码):
While (true)
{
   Body
}

亲切的问候,

Dan


1
此外,这里有一个很好的讨论,探讨更加CPU友好的方法:https://dev59.com/EWs05IYBdhLWcg3wLO6Q - Bart Verkoeijen

5

使用 While 循环

bool userWantsToExit = false;

get input

while(!userWantsToExit)
{

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

program end;

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

1

你可以在程序中将需要执行的操作放入一个循环中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接