2009-11-18 9 views
5

J'ai juste besoin de pouvoir boucler une application de console. ce que je veux dire par là est:Comment boucler une application 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. 

J'espère que cela a du sens. quelqu'un peut-il s'il vous plaît expliquer comment je ferais cela? merci :)

+0

voulez-vous le programme pour quitter/relancer? Pouvez-vous clarifier? – Rippo

+0

Cela ressemble à des devoirs. Publiez un exemple de code sur lequel vous travaillez, si ce n'est pas le cas, nous vous dirons pourquoi. En attendant :), jetez un oeil à Console.Readline. –

+0

@Michael Van Engelen. J'ai terminé le lycée en 2000. J'ai maintenant 24 ans. ;) Je SOUHAITE j'étais de retour à l'école lol –

Répondre

4

Vous pouvez envelopper tout le corps de votre méthode principale de Program.cs dans une boucle while avec une condition qui sera toujours satisfaite.

par exemple (en pseudo-code)

While (true) 
{ 
    Body 
} 

Bonté,

Dan

+0

merci Dan qui a été très apprécié. :) –

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

L'utilisateur peut arrêter le programme à tout moment avec CTRL-C.

Est-ce ce que vous vouliez dire?

1

Vous pouvez simplement mettre une boucle autour de ce que vous faites dans votre programme.

4

Utilisez une boucle While

bool userWantsToExit = false; 

get input 

while(!userWantsToExit) 
{ 

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

program end; 
13
Console.WriteLine("bla bla - enter xx to exit"); 
string line; 
while((line = Console.ReadLine()) != "xx") 
{ 
    string result = DoSomethingWithThis(line); 
    Console.WriteLine(result); 
} 
2
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()); 
     } 
    } 
} 
Questions connexes