我對字符串和int感到困惑。並且不能驗證名稱是沒有數字和奇怪的特徵。 a-z和A-Z都很好。我理解while循環和哨兵的使用。我在這裏看到了正則表達式,但由於某種原因,我的代碼無法工作。我寧願選擇一種我能理解的簡單解決方案。我在我的代碼中驗證int的效果很好,但驗證名稱會得到bool和int錯誤。字符串字母驗證
static void Main(string[] args)
{
int age;
double mileage;
string strInput, name;
bool isValid;
DisplayApplicationInformation();
DisplayDivider("Start Program");
Console.WriteLine();
DisplayDivider("Get Name");
strInput = GetInput("your name");
name = strInput;
Console.WriteLine("Your name is: " + name);
Console.WriteLine();
do
{
DisplayDivider("Get Age");
strInput = GetInput("your age");
isValid = int.TryParse(strInput, out age);
if (!isValid || (age <= 0))
{
isValid = false;
Console.WriteLine("'" + strInput + "' is not a valid age entry. Please retry...");
}
}while (!isValid);
Console.WriteLine("Your age is: " + age);
//age = int.Parse(strInput);
//Console.WriteLine("Your age is: " + age);
Console.WriteLine();
do
{
DisplayDivider("Get Mileage");
strInput = GetInput("gas mileage");
isValid = double.TryParse(strInput, out mileage);
if (!isValid || (mileage <= 0))
{
isValid = false;
Console.WriteLine("'" + strInput + "' is not a valid mileage entry. Please retry...");
}
} while (!isValid);
Console.WriteLine("Your age is: " + mileage);
//mileage = double.Parse(strInput);
//Console.WriteLine("Your car MPT is: " + mileage);
TerminateApplication();
}
向我們展示你試圖驗證名稱是什麼線。 – antlersoft
你能進一步解釋你提到的錯誤嗎?你在代碼中得到了實際的例外嗎?如果是這樣,它們是什麼,它們在哪裏發生,以及它們發生時的數據狀態如何?如果沒有實際的錯誤,錯誤的指示是什麼?預期的行爲與觀察到的行爲有什麼不同?代碼中的哪兩個位置有偏差? – David
如果你可以向自己介紹一下正則表達式,它比你試圖嘗試的簡單得多;) – craig1231