我想检查用户输入是否是" 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("=======================");
}
}
}
答案 0 :(得分:2)
问题是你在while循环之前进行评估。另外,为userKeyPress
分配一个空字符并手动调用showMenue()
方法,所以不能这样做:
char userKeyPress = '-';
showMenue();
while (!menuChars.Contains(userKeyPress))
{
Console.WriteLine("\nBitte gültigen Buchstaben eingeben: ");
userKeyPress = Console.ReadKey().KeyChar;
}