我正在尝试进行 while 循环以确保员工的格式正确 - 但是只有当我第一次正确输入员工 ID 时它才有效。
When I put first a wrong format to the ID and then put a correct one it does not revaluate and identify it as true
Here is the part of the code in question:
Console.Write("Please enter your employee ID:");
empID = Console.ReadLine();
string pattern = @"^\d{9}[A-Z]{1}$";
Match match = Regex.Match(empID, pattern);
while (match.Success != true)
{
if (match.Success == true)
{
Console.WriteLine(empID);
}
else
{
Console.WriteLine("Incorrect employee ID - please try again");
empID = Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
Any idea what it could be that it does not see empID as correct when entered correctly second time?
Thanks
小智 8
You don't update match variable value in the loop. Please see my comments in the code
// here you receive your employee id
Console.Write("Please enter your employee ID:"); empID = Console.ReadLine();
string pattern = @"^\d{9}[A-Z]{1}$";
// here you initialize your match variable by result of matching with regex
Match match = Regex.Match(empID, pattern);
while (match.Success != true)
{
if (match.Success == true)
{
Console.WriteLine(empID);
}
else
{
Console.WriteLine("Incorrect employee ID - please try again");
// here you read your new employee id, but match variable is not updated
empID = Console.ReadLine();
// this is what you missed
match = Regex.Match(empID, pattern);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
116 次 |
| 最近记录: |