If-else 检查所有字符串值(长度,是数字)
If-else check for all string values (length, is numeric)
我用 C# 编写了一个小型控制台应用程序以开始使用该语言。我的目标是向用户询问以下内容:
- 名字
- 姓
- 出生年份
- 出生月份
- 出生日期
我已将所有输入字段设置为如下所示:
System.Console.Write("Name: ");
String name = System.Console.ReadLine();
最后,应用程序将数据保存到 .txt 文件,如果给定的数据有效。我需要检查名称字段的长度是否在 1-30 之间,以及日期输入是否只接受相应限制范围内的数字答案(例如:您只能给 'month' 一个 1-12 之间的值。 .)
我试图搜索不同的验证方法,但我不知道如何将它们全部放在一起并为该应用程序制作一个干净的 "Checker" 部分。
这是验证我的名字和姓氏字段的方法,但我认为您不能对日期字段进行相同的检查?
public static Boolean Checker(String check)
{
if (check.Length <= 30 && check.Length > 0)
{
return true;
}
return false;
}
有什么建议吗?
为了检查输入字符串是否为日期,您可以尝试将其解析为 DateTime 对象:
DateTime date;
if (!DateTime.TryParseExact(inputString, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None, out date))
{
}
如果不知道字符串代表什么,就无法合理地在单个方法中验证这些输入。
首先,我建议您只要求输入一个日期,而不是三个单独的值。将日期输入验证为单个值而不是三个单独的值要容易得多。 NET 库提供了许多方法来通过一次调用 (DateTime.TryParse, DateTime.TryParseExact) 来解析日期。相反,拥有三个独立的输入,需要您复制逻辑来检查闰年、检查月份的最后一天以及由本地化问题引起的日期的许多其他细微方面。
因此,我假设您只询问名字、姓氏和出生日期,并将验证更改为
public static Boolean Checker(String check, bool isDate)
{
if(isDate)
{
DateTime dt;
// Here you could add your formatting locale as you find appropriate
return DateTime.TryParse(check, out dt);
}
else
return check.Length > 0 && check.Length <= 30;
}
这样你的输入应该是这样的
// Infinite loop until you get valid inputs or quit
for(;;)
{
System.Console.Write("Name: ('quit' to stop)");
String name = System.Console.ReadLine();
if(name == "quit") return;
if(!Checker(name, false))
{
// Input not valid, message and repeat
Console.WriteLine("Invalid name length");
continue;
}
System.Console.Write("Date of Birth: (quit to stop)");
String dob = System.Console.ReadLine();
if(dob == "quit") return;
if(!Checker(dob, true))
{
// Input not valid, message and repeat
Console.WriteLine("Invalid name length");
continue;
}
// if you reach this point the input is valid and you exit the loop
break;
}
你应该有一个中央方法,如果它是有效的,从输入创建一个 User
-object,并且有一个方法来检查每个输入类型,如 FirstName
或 DayOfBirth
。例如:
public class User
{
public static User CreateUserFromText(string firstName,string surName,string yearBirth,string monthBirth,string dayBirth)
{
if (firstName == null || surName == null || yearBirth == null || monthBirth == null || dayBirth == null)
throw new ArgumentNullException(); // better tell what argument was null
User user = new User
{
FirstName = firstName.Trim(),
SurName = surName.Trim()
};
bool validInput = IsFirstNameValid(user.FirstName) && IsSurNameValid(user.SurName);
DateTime dob;
if (!validInput || !IsValidDayOfBirth(yearBirth, monthBirth, dayBirth, out dob))
return null;
user.DayOfBirth = dob;
return user;
}
public DateTime DayOfBirth { get; set; }
public string SurName { get; set; }
public string FirstName { get; set; }
private static bool IsFirstNameValid(string firstName)
{
return firstName?.Length >= 1 && firstName?.Length <= 30;
}
private static bool IsSurNameValid(string surName)
{
return surName?.Length >= 1 && surName?.Length <= 30;
}
private static bool IsValidDayOfBirth(string year, string month, string day, out DateTime dayOfBirth)
{
DateTime dob;
string dateStr = $"{year}-{month}-{day}";
bool validDayOfBirth = DateTime.TryParse(dateStr, out dob);
dayOfBirth = validDayOfBirth ? dob : DateTime.MinValue;
return validDayOfBirth;
}
}
我用 C# 编写了一个小型控制台应用程序以开始使用该语言。我的目标是向用户询问以下内容:
- 名字
- 姓
- 出生年份
- 出生月份
- 出生日期
我已将所有输入字段设置为如下所示:
System.Console.Write("Name: ");
String name = System.Console.ReadLine();
最后,应用程序将数据保存到 .txt 文件,如果给定的数据有效。我需要检查名称字段的长度是否在 1-30 之间,以及日期输入是否只接受相应限制范围内的数字答案(例如:您只能给 'month' 一个 1-12 之间的值。 .)
我试图搜索不同的验证方法,但我不知道如何将它们全部放在一起并为该应用程序制作一个干净的 "Checker" 部分。
这是验证我的名字和姓氏字段的方法,但我认为您不能对日期字段进行相同的检查?
public static Boolean Checker(String check)
{
if (check.Length <= 30 && check.Length > 0)
{
return true;
}
return false;
}
有什么建议吗?
为了检查输入字符串是否为日期,您可以尝试将其解析为 DateTime 对象:
DateTime date;
if (!DateTime.TryParseExact(inputString, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None, out date))
{
}
如果不知道字符串代表什么,就无法合理地在单个方法中验证这些输入。
首先,我建议您只要求输入一个日期,而不是三个单独的值。将日期输入验证为单个值而不是三个单独的值要容易得多。 NET 库提供了许多方法来通过一次调用 (DateTime.TryParse, DateTime.TryParseExact) 来解析日期。相反,拥有三个独立的输入,需要您复制逻辑来检查闰年、检查月份的最后一天以及由本地化问题引起的日期的许多其他细微方面。
因此,我假设您只询问名字、姓氏和出生日期,并将验证更改为
public static Boolean Checker(String check, bool isDate)
{
if(isDate)
{
DateTime dt;
// Here you could add your formatting locale as you find appropriate
return DateTime.TryParse(check, out dt);
}
else
return check.Length > 0 && check.Length <= 30;
}
这样你的输入应该是这样的
// Infinite loop until you get valid inputs or quit
for(;;)
{
System.Console.Write("Name: ('quit' to stop)");
String name = System.Console.ReadLine();
if(name == "quit") return;
if(!Checker(name, false))
{
// Input not valid, message and repeat
Console.WriteLine("Invalid name length");
continue;
}
System.Console.Write("Date of Birth: (quit to stop)");
String dob = System.Console.ReadLine();
if(dob == "quit") return;
if(!Checker(dob, true))
{
// Input not valid, message and repeat
Console.WriteLine("Invalid name length");
continue;
}
// if you reach this point the input is valid and you exit the loop
break;
}
你应该有一个中央方法,如果它是有效的,从输入创建一个 User
-object,并且有一个方法来检查每个输入类型,如 FirstName
或 DayOfBirth
。例如:
public class User
{
public static User CreateUserFromText(string firstName,string surName,string yearBirth,string monthBirth,string dayBirth)
{
if (firstName == null || surName == null || yearBirth == null || monthBirth == null || dayBirth == null)
throw new ArgumentNullException(); // better tell what argument was null
User user = new User
{
FirstName = firstName.Trim(),
SurName = surName.Trim()
};
bool validInput = IsFirstNameValid(user.FirstName) && IsSurNameValid(user.SurName);
DateTime dob;
if (!validInput || !IsValidDayOfBirth(yearBirth, monthBirth, dayBirth, out dob))
return null;
user.DayOfBirth = dob;
return user;
}
public DateTime DayOfBirth { get; set; }
public string SurName { get; set; }
public string FirstName { get; set; }
private static bool IsFirstNameValid(string firstName)
{
return firstName?.Length >= 1 && firstName?.Length <= 30;
}
private static bool IsSurNameValid(string surName)
{
return surName?.Length >= 1 && surName?.Length <= 30;
}
private static bool IsValidDayOfBirth(string year, string month, string day, out DateTime dayOfBirth)
{
DateTime dob;
string dateStr = $"{year}-{month}-{day}";
bool validDayOfBirth = DateTime.TryParse(dateStr, out dob);
dayOfBirth = validDayOfBirth ? dob : DateTime.MinValue;
return validDayOfBirth;
}
}