C# 我需要为具有一些策略的控制台应用程序编写一个读取字符串方法
我尝试应用的策略是它必须有3个字符长,必须包括小写字母、大写字母和任意顺序的数字。我试图用它作为一种方法来读取用户为分配而设置的密码。我已经写了尽可能多的内容来消除错误,但我想不出一种方法来应用上述策略。这就是我目前所拥有的。。。任何帮助都将不胜感激C# 我需要为具有一些策略的控制台应用程序编写一个读取字符串方法,c#,string,methods,passwords,visual-studio-2015,C#,String,Methods,Passwords,Visual Studio 2015,我尝试应用的策略是它必须有3个字符长,必须包括小写字母、大写字母和任意顺序的数字。我试图用它作为一种方法来读取用户为分配而设置的密码。我已经写了尽可能多的内容来消除错误,但我想不出一种方法来应用上述策略。这就是我目前所拥有的。。。任何帮助都将不胜感激 private static string readPass() { //readpass method to read in password of 1 lower case, 1 upper case & 1 number
private static string readPass() { //readpass method to read in password of 1 lower case, 1 upper case & 1 number
bool validInput = true;
char letterUserInput; //letter from string
int asciiCodeLetter; //number code for letter
string userInput;
do {
validInput = true;
userInput = Console.ReadLine();
try {
if (userInput.Length < 1) {
validInput = false;
Console.WriteLine("You have just pressed Enter Please Enter a valid Password");
} //if check length
else if (userInput.Length > 1 && userInput.Length < 3) {
validInput = false;
Console.WriteLine("Password Is Too Short. Please Enter Valid Password");
} //if check length too short
else if (userInput.Length > 3)
{
validInput = false;
Console.WriteLine("Password Is Too Long. Please Enter A Valid Password");
} //if check length too short
for (int i = 0; i < userInput.Length; i++) //take each letter in turn from string
{
letterUserInput = userInput[i];
asciiCodeLetter = Convert.ToInt16(letterUserInput);
if (asciiCodeLetter < 48 || asciiCodeLetter > 57)
{
validInput = false;
Console.WriteLine("You have used an invalid character.\n Password Must Contain 1 Upper Case, 1 Lower Case letter and 1 Number)");
} //character check with ASCII
}
}
catch
{
validInput = false;
Console.WriteLine("An unspecified error has occured");
} //catch
if (validInput == false) Console.Write("Invalid Password. Please Enter Again = ");
} while (validInput == false); // do. repeat entry if invalid
return userInput;
}
private static string readPass(){//readPass方法读取1个小写、1个大写和1个数字的密码
布尔有效输入=真;
char letterUserInput;//字符串中的字母
int asciicodeler;//字母的数字代码
字符串用户输入;
做{
validInput=true;
userInput=Console.ReadLine();
试一试{
if(userInput.Length<1){
validInput=假;
Console.WriteLine(“您刚刚按了Enter,请输入有效密码”);
}//如果检查长度
else if(userInput.Length>1&&userInput.Length<3){
validInput=假;
Console.WriteLine(“密码太短,请输入有效密码”);
}//如果检查长度太短
else if(userInput.Length>3)
{
validInput=假;
Console.WriteLine(“密码太长,请输入有效密码”);
}//如果检查长度太短
for(inti=0;i57)
{
validInput=假;
Console.WriteLine(“您使用了无效字符。\n密码必须包含1个大写字母、1个小写字母和1个数字)”;
}//使用ASCII进行字符检查
}
}
抓住
{
validInput=假;
Console.WriteLine(“发生了未指定的错误”);
}//抓住
if(validInput==false)Console.Write(“密码无效,请重新输入=”);
}while(validInput==false);//执行。如果无效,请重复输入
返回用户输入;
}
这方面的一些伪代码可能是:
String candidatePwd = textboxPwd.Text.Trim();
Boolean validPwd = IsValidPassword(candidatePwd);
private bool IsValidPassword(String candidate)
{
return ((candidate.Length == 3) &&
(ContainsOneUppercaseLetter(candidate)) &&
(ContainsOneLowerCaseLetter(candidate)) &&
(ContainsOneNumber(candidate)));
}
private bool ContainsOneUppercaseLetter(String candidate)
{
// search for an uppercase letter; if one found, return true
}
private bool ContainsOneLowercaseLetter(String candidate)
{
// search for a lowercase letter; if one found, return true
}
private bool ContainsOneUppercaseLetter(String candidate)
{
// search for a number; if one found, return true
}
实际实现留给您。这方面的一些伪代码可能是:
String candidatePwd = textboxPwd.Text.Trim();
Boolean validPwd = IsValidPassword(candidatePwd);
private bool IsValidPassword(String candidate)
{
return ((candidate.Length == 3) &&
(ContainsOneUppercaseLetter(candidate)) &&
(ContainsOneLowerCaseLetter(candidate)) &&
(ContainsOneNumber(candidate)));
}
private bool ContainsOneUppercaseLetter(String candidate)
{
// search for an uppercase letter; if one found, return true
}
private bool ContainsOneLowercaseLetter(String candidate)
{
// search for a lowercase letter; if one found, return true
}
private bool ContainsOneUppercaseLetter(String candidate)
{
// search for a number; if one found, return true
}
实际实现由您决定。您可以使用正则表达式来实现:
var input = Console.ReadLine();
var regex = new System.Text.RegularExpressions.Regex("^([0-9][a-z][A-Z]|[0-9][A-Z][a-z]|[a-z][0-9][A-Z]|[a-z][A-Z][0-9]|[A-Z][a-z][0-9]|[A-Z][0-9][a-z])$");
validInput = input != null && regex.IsMatch(input);
我肯定有更多的高级正则表达式。但如果您不熟悉正则表达式,这就足以理解它
编辑
如果您想要更高级的东西,可以使用以下正则表达式:
var input = Console.ReadLine();
var regex = new System.Text.RegularExpressions.Regex(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{3}$");
validInput = input != null && regex.IsMatch(input);
您可以为此使用正则表达式:
var input = Console.ReadLine();
var regex = new System.Text.RegularExpressions.Regex("^([0-9][a-z][A-Z]|[0-9][A-Z][a-z]|[a-z][0-9][A-Z]|[a-z][A-Z][0-9]|[A-Z][a-z][0-9]|[A-Z][0-9][a-z])$");
validInput = input != null && regex.IsMatch(input);
我肯定有更多的高级正则表达式。但如果您不熟悉正则表达式,这就足以理解它
编辑
如果您想要更高级的东西,可以使用以下正则表达式:
var input = Console.ReadLine();
var regex = new System.Text.RegularExpressions.Regex(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{3}$");
validInput = input != null && regex.IsMatch(input);
对于3个字符-只需将用户编写的密码保存为字符串,然后检查其长度。如果是大写,我会转换ASCII码上的每个字符,然后检查是否有任何代码等于所有大写字母的代码。要检查是否有数字,请使用以下内容检查密码的每个字符 int n;bool isNumeric=int.TryParse(“123”,out n)
希望这会有所帮助。对于3个字符,只需将用户编写的密码保存为字符串,然后检查其长度。如果是大写,我会转换ASCII码上的每个字符,然后检查是否有任何代码等于所有大写字母的代码。要检查是否有数字,请使用以下内容检查密码的每个字符 int n;bool isNumeric=int.TryParse(“123”,out n)
希望这会有所帮助。您可以使用正则表达式: ^.(?=.{3,})(?=.\d)(?=[a-z])(?=[a-z])*$ 并简化您的代码:
if(!Regex.IsMatch(userInput, "^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$")
Console.WriteLine("The password has to be 3 characters long, and must include a lower case letter, an upper case letter, and a number");
您可以使用正则表达式: ^.(?=.{3,})(?=.\d)(?=[a-z])(?=[a-z])*$ 并简化您的代码:
if(!Regex.IsMatch(userInput, "^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$")
Console.WriteLine("The password has to be 3 characters long, and must include a lower case letter, an upper case letter, and a number");
您可以在第二个else中删除
userInput.Length>1&&
。因为只有当第一个不满足时才会执行该块。为什么不尝试此链接。。有一个c#示例和一个使用Regex
的1行程序示例,您可以在第二个else中删除userInput.Length>1&
。因为只有当第一个不满足时才会执行该块。为什么不尝试此链接。。使用Regex