Fixed logic error in password loop

This commit is contained in:
Mark J Price 2023-04-06 08:16:14 +01:00
parent 84f4e5640f
commit ea406c2372
2 changed files with 12 additions and 6 deletions

View file

@ -10,7 +10,9 @@ while (x < 10)
// Looping with the do statement
string? actualPassword = "Pa$$w0rd";
string? password;
int maximumAttempts = 10;
int attempts = 0;
do
@ -19,15 +21,16 @@ do
Write("Enter your password: ");
password = ReadLine();
}
while ((password != "Pa$$w0rd") & (attempts < 10));
while ((password != actualPassword) & (attempts < maximumAttempts));
if (attempts < 10)
if (password == actualPassword)
{
WriteLine("Correct!");
}
else
{
WriteLine("You have used 10 attempts!");
WriteLine("You have used {0} attempts! The password was {1}.",
arg0: maximumAttempts, arg1: actualPassword);
}
// Looping with the for statement

View file

@ -10,7 +10,9 @@ while (x < 10)
// Looping with the do statement
string? actualPassword = "Pa$$w0rd";
string? password;
int maximumAttempts = 10;
int attempts = 0;
do
@ -19,15 +21,16 @@ do
Write("Enter your password: ");
password = ReadLine();
}
while ((password != "Pa$$w0rd") & (attempts < 10));
while ((password != actualPassword) & (attempts < maximumAttempts));
if (attempts < 10)
if (password == actualPassword)
{
WriteLine("Correct!");
}
else
{
WriteLine("You have used 10 attempts!");
WriteLine("You have used {0} attempts! The password was {1}.",
arg0: maximumAttempts, arg1: actualPassword);
}
// Looping with the for statement