首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >存在代码无法识别输入的密码是否正确的问题

存在代码无法识别输入的密码是否正确的问题
EN

Stack Overflow用户
提问于 2021-11-13 03:33:08
回答 2查看 53关注 0票数 0

我有一个问题,使程序能够理解用户输入的答案是否正确。我试着用口令代替,但没有起作用。我现在非常困惑为什么这不起作用,我也尝试了userInput = String.Empty,反之亦然,但这使得它的答案总是正确的,老实说,我想不出其他任何东西了这是我的代码现在的样子:

代码语言:javascript
运行
复制
       string password = "abcd1234";
        string userInput = String.Empty;
        string Text1 = "Please Insert Your Password";
        string Text2 = "Incorrect Password!";
        string Text3 = "Correct You May Proceed!";

         
        Console.WriteLine(Text1);
        Console.ReadLine();
        if (userInput == password)
            Console.WriteLine(Text3);            
        
        
        if (userInput != password)
            Console.WriteLine(Text2);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-13 09:24:41

请使用以下命令:

代码语言:javascript
运行
复制
        string password = "abcd1234";
        string Text1 = "Please Insert Your Password";
        string Text2 = "Incorrect Password!";
        string Text3 = "Correct You May Proceed!";
        string userInput = Console.ReadLine();
        Console.WriteLine(Text1);
        if (password.Equals(userInput))
            Console.WriteLine(Text3);
        else 
            Console.WriteLine(Text2);
票数 -1
EN

Stack Overflow用户

发布于 2021-11-13 03:42:03

我可以假设您在看到Text1提示符之前也必须按enter键吗?

string userInput = Console.ReadLine();在你的应用程序中看到任何其他东西之前都会监听你的输入。提示输入密码后,应将此行移动到。

应删除另一个ReadLine:

代码语言:javascript
运行
复制
    Console.ReadLine(); \\this doesn't assign your input to anything

此行正在等待输入,但不会对其执行任何操作。

顺便说一句,你确实想要'==‘--如果两者相等,它将返回true。单个'=‘用于赋值,就像您在前面的代码中所做的那样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69951239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档