我知道我错过了一些非常微不足道的东西,但我无法找到它。这是代码:
class Program
{
static void Main(string[] args)
{
string username = "";
string pass = "";
string confirmPass = "";
Console.Write("Enter username: ");
username = Console.ReadLine();
Console.WriteLine();
Console.Write("Enter your password: ");
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
// Backspace Should Not Work
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
pass += key.KeyChar;
Console.Write("*");
}
else if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
{
pass = pass.Substring(0, (pass.Length - 1));
Console.Write("\b \b");
}
// Stops Receving Keys Once Enter is Pressed
} while (key.Key != ConsoleKey.Enter);
Console.WriteLine("");
Console.WriteLine("Confirm your password: ");
ConsoleKeyInfo confirmKey;
int retryCount = 3;
string finalPass = "";
do{
confirmKey = Console.ReadKey(true);
// Backspace Should Not Work
if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
{
confirmPass += confirmKey.KeyChar;
Console.Write("*");
}
else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
{
confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
Console.Write("\b \b");
}
//
// Stops Receving Keys Once Enter is Pressed
} while ((confirmKey.Key != ConsoleKey.Enter));
//Console.WriteLine("");
//string confirmPass = "";
do
{
if (confirmPass != pass)
{
Console.WriteLine("Re-enter Password: (" + retryCount + " tries remaining)");
do
{
confirmKey = Console.ReadKey(true);
// Backspace Should Not Work
if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
{
confirmPass += confirmKey.KeyChar;
Console.Write("*");
}
else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
{
confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
Console.Write("\b \b");
}
//
// Stops Receving Keys Once Enter is Pressed
} while ((confirmKey.Key != ConsoleKey.Enter));
retryCount--;
}
else if (confirmPass == pass)
{
Console.WriteLine("Enter password to log in :");
finalPass = Console.ReadLine();
if (finalPass == pass)
{
Console.WriteLine("Login Successful. Welcome " + username + "!");
Console.WriteLine();
Console.WriteLine("Test Successful. Press Enter to quit.");
}
}
if (retryCount == 0)
{
Console.WriteLine("Exceeded number of tries!!");
Console.ReadLine();
}
} while (confirmPass != pass && retryCount != 0);
}
}
答案 0 :(得分:0)
根据您的代码,这就是我想出的:
public static void Main(string[] args)
{
string username = "";
string pass = "";
string confirmPass = "";
Console.Write("Enter username: ");
username = Console.ReadLine();
Console.WriteLine();
Console.Write("Enter your password: ");
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
// Backspace Should Not Work
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
pass += key.KeyChar;
Console.Write("*");
}
else if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
{
pass = pass.Substring(0, (pass.Length - 1));
Console.Write("\b \b");
}
// Stops Receving Keys Once Enter is Pressed
} while (key.Key != ConsoleKey.Enter);
Console.WriteLine("");
Console.WriteLine("Confirm your password: ");
ConsoleKeyInfo confirmKey;
int retryCount = 3;
string finalPass = "";
do
{
confirmKey = Console.ReadKey(true);
// Backspace Should Not Work
if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
{
confirmPass += confirmKey.KeyChar;
Console.Write("*");
}
else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
{
confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
Console.Write("\b \b");
}
// Stops Receving Keys Once Enter is Pressed
} while ((confirmKey.Key != ConsoleKey.Enter));
do
{
if (confirmPass != pass)
{
confirmPass = "";
Console.WriteLine("Re-enter Password: (" + retryCount + " tries remaining)");
do
{
confirmKey = Console.ReadKey(true);
// Backspace Should Not Work
if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
{
confirmPass += confirmKey.KeyChar;
Console.Write("*");
}
else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
{
confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
Console.Write("\b \b");
}
// Stops Receving Keys Once Enter is Pressed
} while ((confirmKey.Key != ConsoleKey.Enter));
retryCount--;
}
else if (confirmPass == pass)
{
Console.WriteLine("Enter password to log in :");
finalPass = Console.ReadLine();
if (finalPass == pass)
{
Console.WriteLine("Login Successful. Welcome " + username + "!");
Console.WriteLine();
Console.WriteLine("Test Successful. Press Enter to quit.");
Console.ReadLine();
break;
}
}
if (retryCount == 0)
{
Console.WriteLine("Exceeded number of tries!!");
Console.ReadLine();
}
} while (retryCount != 0);
}
基本上我做了以下更改:
do
循环并输入第一个if
语句时,我设置了confirmPass = "";
。这就是您的代码最初没有执行此操作的原因,因为您将新 confirmPass
附加到原始代码中。因此,如果他们输入password
,然后输入password2
,则重试他们输入password
,confirmPass
为password2password
。confirmPass != pass &&
中删除了while
位。这使您可以进入该循环中的elseif
部分。elseif
它就会无限循环。所以我告诉break
里面elseif
杀死那个do/while
循环。如果此解决方案不正确,请澄清您的要求,以便我们更好地为您提供帮助。
答案 1 :(得分:0)
您的程序相当复杂,这就是为什么发现任何错误会更加困难的原因。您应该真正学习如何使用子例程(即函数),这样您就不必重复一遍又一遍地执行相同操作的代码块(例如读取密码)。
如果我们稍微重构一下代码,可以将这些重复的部分移到一个名为ReadPassword
的方法中,如下所示:
public static string ReadPassword()
{
var pass = "";
while (true)
{
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
// Stop Receving Keys Once Enter is Pressed
break;
// Backspace Should Not Work
if (key.Key != ConsoleKey.Backspace)
{
pass += key.KeyChar;
Console.Write("*");
}
else if (pass.Length > 0)
{
pass = pass.Substring(0, (pass.Length - 1));
Console.Write("\b \b");
}
}
return pass;
}
并用它来替换用于读取密码的所有do ... while
循环,而不更改代码中的任何其他内容(因此错误仍然存在):
public static void Main(params string[] args)
{
string username = "";
string pass = "";
string confirmPass = "";
Console.Write("Enter username: ");
username = Console.ReadLine();
Console.WriteLine();
Console.Write("Enter your password: ");
pass = ReadPassword(); // changed to function call
Console.WriteLine("");
Console.WriteLine("Confirm your password: ");
int retryCount = 3;
string finalPass = "";
confirmPass = ReadPassword(); // changed to function call
do
{
if (confirmPass != pass) // <==== this is performed before the "else"
{
Console.WriteLine("Re-enter Password: (" + retryCount + " tries remaining)");
confirmPass = ReadPassword(); // changed to function call
retryCount--;
}
else if (confirmPass == pass) // <==== "else": bug
{
// Bug: it will only get here, if the confirmed password
// was entered correctly the first time
Console.WriteLine("Enter password to log in :");
finalPass = Console.ReadLine();
if (finalPass == pass)
{
Console.WriteLine("Login Successful. Welcome " + username + "!");
Console.WriteLine();
Console.WriteLine("Test Successful. Press Enter to quit.");
}
// else <==== now what?
}
if (retryCount == 0)
{
Console.WriteLine("Exceeded number of tries!!");
Console.ReadLine();
}
} while (confirmPass != pass && retryCount != 0);
}
现在,错误变得更容易被发现。我在评论中使用<===
突出显示了它们。我会留给你弄清楚出了什么问题,以及你如何解决它。