2009-11-18 20 views
5

Devo solo essere in grado di eseguire il loop di un'app console. quello che voglio dire è:Come eseguire il ciclo di un'app 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. 

spero che abbia un senso. qualcuno può spiegare come andrei a fare questo? grazie :)

+0

si desidera che il programma per uscire/riavviare? Puoi chiarire? – Rippo

+0

Sembra un compito a casa. Pubblica un esempio di codice su cui stai lavorando, altrimenti, ti diremo perché. Nel frattempo :), dai un'occhiata a Console.Readline. –

+0

@ Michael van Engelen. Ho finito il liceo nel 2000. Ora ho 24 anni. ;) DESIDERO che ero tornato a scuola lol –

risposta

4

Si potrebbe avvolgere l'intero corpo del metodo principale in Program.cs in un ciclo while con una condizione che sarà sempre soddisfatta.

per esempio (in pseudo-codice)

While (true) 
{ 
    Body 
} 

Gentilezza,

Dan

+0

grazie Dan che è stato molto apprezzato. :) –

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

L'utente può interrompere il programma in qualsiasi momento con CTRL-C.

È questo che intendevi?

1

Puoi semplicemente aggiungere un ciclo a quello che stai facendo nel programma.

4

utilizzare un ciclo 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()); 
     } 
    } 
} 
Problemi correlati