2009-11-18 13 views
5

मुझे बस एक कंसोल ऐप लूप करने में सक्षम होना चाहिए। मेरा मतलब यह है कि:कंसोल ऐप को कैसे लूप करें

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. 

मुझे उम्मीद है कि यह समझ में आया। क्या कोई यह बता सकता है कि मैं ऐसा करने के बारे में कैसे जाऊंगा? धन्यवाद :)

+0

आप इस कार्यक्रम बाहर निकलने के लिए/पुनः आरंभ करना चाहते हैं? क्या आप स्पष्ट कर सकते हो? – Rippo

+0

यह होमवर्क जैसा दिखता है। जिस कोड पर आप काम कर रहे हैं उसका एक उदाहरण पोस्ट करें, अगर ऐसा नहीं होता है, तो हम आपको बताएंगे क्यों। इस बीच :), कंसोल पर एक नज़र डालें। रीडलाइन। –

+0

@ माइकल वान एंजेलन। मैंने 2000 में हाईस्कूल समाप्त किया। अब मैं 24 साल का हूँ। ;) मुझे लगता है कि मैं स्कूल लॉल –

उत्तर

4

का प्रयोग करें आप एक शर्त है कि हमेशा संतुष्ट होंगे साथ थोड़ी देर के पाश में Program.cs में अपने मुख्य विधि के पूरे शरीर में लपेट कर सकते हैं।

उदा

While (true) 
{ 
    Body 
} 

दया (छद्म कोड में),

दान

+0

पर वापस आया था धन्यवाद दान जिसकी बहुत सराहना की गई थी। :) –

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

उपयोगकर्ता CTRL-C साथ किसी भी बिंदु पर कार्यक्रम रोक सकता है।

क्या यह आपका मतलब है?

1

आप अपने प्रोग्राम में जो कुछ भी कर रहे हैं उसके आसपास बस एक लूप लगा सकते हैं।

4

एक ओर जहां पाश

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()); 
     } 
    } 
} 
संबंधित मुद्दे