2009-11-18 28 views
5

tôi chỉ cần có thể lặp lại ứng dụng bảng điều khiển. ý tôi là:Cách lặp lại một Ứng dụng 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. 

tôi hy vọng điều đó có ý nghĩa. bất cứ ai có thể vui lòng giải thích làm thế nào tôi sẽ đi về việc này? cảm ơn bạn :)

+0

nào bạn muốn chương trình để thoát/khởi động lại? Bạn có thể làm rõ? – Rippo

+0

Điều này trông giống như bài tập về nhà. Đăng một ví dụ về mã bạn đang làm việc, nếu không, chúng tôi sẽ cho bạn biết lý do. Trong thời gian chờ đợi :), hãy xem Console.Readline. –

+0

@Michael Van Engelen. Tôi đã học xong trung học năm 2000. Tôi bây giờ đã 24 tuổi. ;) Tôi mong tôi đã trở lại trường học lol –

Trả lời

4

Bạn có bọc toàn bộ cơ thể của chính phương pháp của bạn trong program.cs trong một lặp while với một tình trạng mà sẽ luôn được hài lòng.

ví dụ (trong pseudo-code)

While (true) 
{ 
    Body 
} 

Kindness,

Dan

+0

cảm ơn bạn Dan đã được rất nhiều đánh giá cao. :) –

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

Người dùng có thể dừng chương trình tại bất kỳ thời điểm nào với CTRL-C.

Đây có phải là ý của bạn không?

1

Bạn chỉ có thể đặt một vòng lặp xung quanh những gì bạn đang làm trong chương trình của mình.

4

Sử dụng một vòng lặp khi

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()); 
     } 
    } 
} 
Các vấn đề liên quan