1
我想限制用戶的輸入,所以他只能輸入28,29,30或31如何限制用戶輸入?
我知道,有一種方法通過檢查這樣做,如果輸入的是在有效日期的數組/天。有人可以解釋我怎麼做一張支票?例如,如果
INT []天=新INT [4] {28,29,30,31};
我該怎麼辦驗證,如果輸入的是什麼用戶在陣列內,將檢查?我應該設置什麼條件?由於我不知道如何編寫這種類型的驗證,因此我沒有要顯示的代碼。如果沒有辦法做到這一點,如何限制用戶使用if語句來限制他只有這4個數字?
到目前爲止我的代碼看起來像這樣:
int GetDaysInMonth()
{
const int MinDaysInMonth = 28;
const int MaxDaysInMonth = 31;
Console.WriteLine("How many days there are in your chosen month? [it needs to be 28, 30 or 31]");
userInput = Console.ReadLine();
while (!int.TryParse(userInput, out daysInMonth) || daysInMonth > MaxDaysInMonth || daysInMonth < MinDaysInMonth)
{
Console.WriteLine("Wrong input! Remember it needs to be 28, 30 or 31");
userInput = Console.ReadLine();
}
Console.WriteLine();
return daysInMonth;
}
感謝
你有什麼代碼,你要限制對輸入?這是一個控制檯應用程序? Web應用程序? Winforms應用程序? – MattD
控制檯應用程序 – Xarels
我已經添加一段代碼,我想驗證爲本人如上所述。 – Xarels