2009-11-18 13 views
5

solo necesito poder hacer un bucle en una aplicación de consola. lo que quiero decir con eso es:Cómo hacer un bucle en una aplicación de consola

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. 

espero que tenga sentido. ¿Alguien puede explicar cómo voy a hacer esto? gracias :)

+0

¿Quieres que el programa salga/reinicio? ¿Puedes aclarar? – Rippo

+0

Esto parece una tarea. Publique un ejemplo del código en el que está trabajando, si no lo hace, le diremos por qué. Mientras tanto :), eche un vistazo a Console.Readline. –

+0

@Michael Van Engelen. Terminé la escuela secundaria en 2000. Tengo ahora 24 años. ;) DESEO que volviera a la escuela jajaja –

Respuesta

4

Usted puede envolver todo el cuerpo de su método principal en Program.cs en un bucle while con una condición que siempre estará satisfecho.

por ejemplo (en pseudo-código)

While (true) 
{ 
    Body 
} 

Bondad,

Dan

+0

gracias Dan, eso fue muy apreciado. :) –

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

El usuario puede detener el programa en cualquier momento con CTRL-C.

¿Es esto lo que quieres decir?

1

Puede simplemente poner un círculo alrededor de lo que sea que esté haciendo en su programa.

4

utilizar un bucle Mientras

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