检查用户输入密码(字符)的程序没有给出正确的结果,C#

检查用户输入密码(字符)的程序没有给出正确的结果,C#,c#,passwords,chars,C#,Passwords,Chars,我试图让程序对照密码“prog”检查chars中的用户条目。用户有三次尝试获取正确的密码。然而,它通常会给出“正确密码”的答案,尽管它不匹配 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;   namespace ConsoleApplication10 {     class Program     {    

我试图让程序对照密码“prog”检查chars中的用户条目。用户有三次尝试获取正确的密码。然而,它通常会给出“正确密码”的答案,尽管它不匹配

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            int tries = 0;
            bool valid = false;
            bool p, r, o, g;
            char userInput;
            int characters;
 
            while (tries < 3 && valid == false)
            {
                Console.WriteLine("Please enter password:");
                p = r = o = g = false;
                characters = 0;
                while (characters < 4)
                {
                    userInput = Console.ReadKey().KeyChar;
                    if (userInput == 'p' || userInput == 'P') p = true;
                    else if (userInput == 'r' || userInput == 'R') r = true;
                    else if (userInput == 'o' || userInput == 'O') o = true;
                    else if (userInput == 'g' || userInput == 'G') g = true;
                    characters++;
                }
 
                if (p == r == o == g == true)
                {
                    valid = true;
                }
                tries++;
            }
 
            if (valid == true)
            {
                Console.WriteLine("\nright password");
            }
            else Console.WriteLine("\nwrong password");
            Console.ReadLine();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
 
命名空间控制台应用程序10
{
班级计划
    {
静态void Main(字符串[]参数)
        {
int=0;
bool valid=false;
布尔p,r,o,g;
字符用户输入;
整数字符;
 
while(尝试<3&&valid==false)
            {
Console.WriteLine(“请输入密码:”);
p=r=o=g=false;
字符=0;
while(字符数<4)
                {
userInput=Console.ReadKey().KeyChar;
如果(userInput='p'| | userInput=='p')p=true;
如果(userInput=='r'| | userInput=='r')r=true;
如果(userInput='o'| | userInput=='o')o=true;
如果(userInput=='g'| | userInput=='g')g=true;
字符++;
                }
 
如果(p==r==o==g==true)
                {
有效=真;
                }
尝试++;
            }
 
如果(有效==true)
            {
Console.WriteLine(“\n正确密码”);
            }
else Console.WriteLine(“\n错误密码”);
Console.ReadLine();
        }
    }
}

将if语句更改为

if (p == true && r == true &&  o == true && g == true)
{
    valid = true;
}
或者,由于您的布尔值设置为true,您可以编写如下内容,因此无需编写true:

if(p && r && o && g) valid = true;
Id将程序更改为类似以下内容:

        var tries = 0;

        while (tries < 3)
        {

            Console.Write("Please enter password: ");                
            var userInput = Console.ReadLine();
            if (userInput == "prog")
            {
                Console.WriteLine("\nright password");
                Console.ReadKey();
                break;
            }
                Console.WriteLine("\nwrong password, please press enter to try again");
                Console.ReadKey();
                Console.Clear();

                tries++;      
       }  
var=0;
while(尝试<3次)
{
控制台。写入(“请输入密码:”);
var userInput=Console.ReadLine();
如果(用户输入=“程序”)
{
Console.WriteLine(“\n正确密码”);
Console.ReadKey();
打破
}
Console.WriteLine(“\n错误的密码,请按enter重试”);
Console.ReadKey();
Console.Clear();
尝试++;
}  

“然而,尽管密码不匹配,但它通常会给出“正确的密码”的答案。”——你能举个例子吗?从代码判断,“p”、“r”、“o”和“g”的任何组合都会通过,因为您没有检查它们的顺序,只要它们存在。@zom4您是想看看它们输入的是整个单词prog还是字母这几乎是我见过的最糟糕的密码验证代码seen@zom4你需要使用chars吗?我同意,但我可能会选择:如果(p&&r&&o&&g){…}非常感谢,它现在起作用了!任务是用字符来完成,这样输入“PRoG”或“ropr”仍然有效,因为字母是相同的,所以我不能将其与“PRoG”进行比较。但是这种方法很有效,谢谢@zom4没有问题如果它工作,请接受答案:)