2014-01-14 60 views
0

我製作了一個控制檯應用程序,用於計算自用戶指定日期以來的天數。但是在原始計算之後,如果輸入了另一個日期,則應用程序關閉。獲取控制檯應用程序以允許多次輸入

有沒有辦法讓我的應用程序不關閉,如果用戶想繼續使用它?

Console.WriteLine("Please enter the date you wish to specify: (DD/MM/YYYY)"); 
     string userInput; 
     userInput = Console.ReadLine(); 
     DateTime calc = DateTime.Parse(userInput); 
     TimeSpan days = DateTime.Now.Subtract(calc); 
     Console.WriteLine(days.TotalDays); 
     Console.ReadLine(); 
+1

請張貼您正在使用的代碼,以便我們能夠最好地告訴您如何修改它並解釋您出錯的位置。 – mclark1129

+1

顯示您的代碼! –

+0

道歉,代碼現在在那裏! – RayB151

回答

3

實現while循環:

Console.WriteLine("Please enter the date you wish to specify: (DD/MM/YYYY)"); 
string userInput; 
userInput = Console.ReadLine(); 
while (userInput != "0") 
{ 
    DateTime calc = DateTime.Parse(userInput); 
    TimeSpan days = DateTime.Now.Subtract(calc); 
    Console.WriteLine(days.TotalDays); 
    Console.WriteLine("Add another date"); 
    userInput = Console.ReadLine(); 
} 

按0,進入退出。

3

將代碼放入循環中,並讓用戶有辦法退出應用程序。

例如

Console.WriteLine("Press q to quit"); 

bool quitFlag = false; 
while (!quitFlag) 
{ 
    Console.WriteLine("Please enter the date you wish to specify: (DD/MM/YYYY)"); 
    string userInput; 
    userInput = Console.ReadLine(); 
    if (userInput == "q") 
    { 
     quitFlag = true; 
    } 
    else 
    { 
     DateTime calc = DateTime.Parse(userInput); 
     TimeSpan days = DateTime.Now.Subtract(calc); 
     Console.WriteLine(days.TotalDays); 
     Console.ReadLine(); 
    } 
} 

這將允許用戶通過輸入「q」退出該應用程序。

+0

這很好,謝謝! – RayB151

相關問題