2015-10-29 20 views
1

我想檢查用戶輸入是否是來自「menuChars」的字符之一,並重復請求,只要它不合適。這是我的完整代碼。我用「===」標記了實際問題。如果您發現其他問題或表現改善,您也可以告訴我。發送請求,直到用戶輸入爲預定值

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Globalization; 

namespace AppointmentBook 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 
     char userKeyPress = showMenue(); 
     char[] menuChars = { 'e', 'E', 'l', 'L', 'k', 'K', 't', 'T', 's', 'S', 'b', 'B' }; 

     //Console.WriteLine("\n" + userKeyPress); 


     Calendar calendarObj = new Calendar(); 

    // ======================= problem area ===================================== 

     if (menuChars.Contains(userKeyPress)) 
     { 
      do 
      { 
      Console.WriteLine("\nBitte gültigen Buchstaben eingeben: "); 
      userKeyPress = Console.ReadKey().KeyChar; 
      } 
      while 
      (menuChars.Contains(userKeyPress)); 
     } 

    // ============================================================ 

     // if START 
     if (userKeyPress == 'e' || userKeyPress == 'E') 
     { 
     calendarObj.addMeeting(); 
     } 

     if (userKeyPress == 'l' || userKeyPress == 'L') 
     { 
     calendarObj.deleteMeeting(); 
     } 

     if (userKeyPress == 'k' || userKeyPress == 'K') 
     { 
     calendarObj.showCalendar(); 
     } 

     if (userKeyPress == 't' || userKeyPress == 'T') 
     { 
     calendarObj.listMeetings(); 
     } 

     if (userKeyPress == 's' || userKeyPress == 'S') 
     { 
     calendarObj.statistic(); 
     } 

     if (userKeyPress == 'b' || userKeyPress == 'B') 
     { 
     calendarObj.endProgram(); 
     } 
    } 

    static public char showMenue() 
    { 
     ConsoleKeyInfo readInput; 

     Console.ForegroundColor = ConsoleColor.White; 
     Console.Write("\nWillkommen im Kalender!"); 

     Console.BackgroundColor = ConsoleColor.DarkBlue; 
     Console.ForegroundColor = ConsoleColor.White; 
     Console.WriteLine("\n\nDatum heute: {0:d}", DateTime.Now); 
     Console.BackgroundColor = ConsoleColor.Black; 

     Console.Write("\nMenü\n"); 

     Console.Write("\n - Termin (e)intragen"); 
     Console.Write("\n - Termin (l)öschen"); 
     Console.Write("\n - (K)alender anzeigen"); 
     Console.Write("\n - (T)erminliste ausgeben"); 
     Console.Write("\n - (S)tatistik"); 
     Console.Write("\n - (B)eenden"); 

     Console.Write("\n\nIhre Wahl: "); 

     readInput = Console.ReadKey(); 
     return readInput.KeyChar; 
    } 
    } 



    class Calendar 
    { 
    public Calendar() //Konstruktor 
    { 

    } 

    public void addMeeting() 
    { 
    var dateFormats = new[] {"dd.MM.yy", "dd-MM-yy", "dd/MM/yy"}; 

    Console.Write("\n\nHinweis: Erlaubes Datumformat: TT.MM.YYYY"); 
    Console.Write("\nDatum für Termin festlegen: "); 

    string readAddMeeting = Console.ReadLine(); 
    DateTime scheduleDate; 

    bool validDate = DateTime.TryParseExact(
     readAddMeeting, 
     dateFormats, 
     DateTimeFormatInfo.InvariantInfo, 
     DateTimeStyles.None, 
     out scheduleDate); 

    if (validDate) 
     { 
     Console.WriteLine("Das Datum {0} ist gültig.", scheduleDate.ToShortDateString()); 
     } 
    else 
     { 
     Console.ForegroundColor = ConsoleColor.Red; 
     Console.WriteLine("-- Fehler! -- Ungültiges Datumformat: \"{0}\"", readAddMeeting); 
     } 
    } 

    public void deleteMeeting() 
    { 
     Console.WriteLine("Termin löschen"); 
     Console.WriteLine("======================="); 
    } 

    public void /*Rückgabetyp*/ showCalendar() // Funktion 
    { 
     Console.WriteLine("Kalender anzeigen"); 
     Console.WriteLine("======================="); 
    } 

    public void listMeetings() 
    { 
     Console.WriteLine("Terminliste ausgeben"); 
     Console.WriteLine("======================="); 
    } 

    public void statistic() 
    { 
     Console.WriteLine("Statistik anzeigen"); 
     Console.WriteLine("======================="); 
    } 

    public void endProgram() 
    { 
     Console.WriteLine("Programm beenden"); 
     Console.WriteLine("======================="); 
    } 
    } 
} 

回答

2

問題是你正在做while循環前的評估。另外分配一個空字符userKeyPress和手動調用showMenue()方法,所以不能這樣做,而不是:

是完美工作
char userKeyPress = '-'; 
showMenue(); 
while (!menuChars.Contains(userKeyPress)) 
{ 
    Console.WriteLine("\nBitte gültigen Buchstaben eingeben: "); 
    userKeyPress = Console.ReadKey().KeyChar; 
} 
+0

一個答案。謝謝!我刪除了showMenue();從你的代碼。否則,每次輸入錯誤的字符時都會彈出整個菜單。 – user5462581

+0

它不應該?你確定這不是因爲你已經在頂部宣佈它? – JonE

+1

好吧,我像你想象的那樣做了它。但兩者都很好。 – user5462581