How to loop a Console App

2019-01-12 01:24发布

i just need to be able to loop a console app. what i mean by that is:

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.

i hope that made sense. can anyone please explain how i would go about doing this? thank you :)

6条回答
【Aperson】
2楼-- · 2019-01-12 01:28
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());
        }
    }
}
查看更多
来,给爷笑一个
3楼-- · 2019-01-12 01:33

You could wrap the whole body of your Main method in program.cs in a while loop with a condition that will always be satisfied.

E.g (in pseudo-code)

While (true)
{
   Body
}

Kindness,

Dan

查看更多
狗以群分
4楼-- · 2019-01-12 01:35
Console.WriteLine("bla bla - enter xx to exit");
string line;
while((line = Console.ReadLine()) != "xx")
{
  string result = DoSomethingWithThis(line);
  Console.WriteLine(result);
}
查看更多
Fickle 薄情
5楼-- · 2019-01-12 01:35

You can just put a loop around whatever you're doing in your program.

查看更多
Root(大扎)
6楼-- · 2019-01-12 01:38
while(true) {
  DisplayText();
  GetInput();
  DoCalculation();
  DisplayResult();
  DisplayText();
  GetInput();
}

The user can stop the program at any point with CTRL-C.

Is this what you meant?

查看更多
对你真心纯属浪费
7楼-- · 2019-01-12 01:51

Use a While loop

bool userWantsToExit = false;

get input

while(!userWantsToExit)
{

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

program end;
查看更多
登录 后发表回答