【问题标题】:How can I make sure user input must be the same input I need from my array如何确保用户输入必须与我需要的数组输入相同
【发布时间】:2022-11-21 22:36:43
【问题描述】:

我是初学者。我有这个问题我不确定我是否能够充分解释它但让我们看看:

我有一个名为 userid 的数组和另一个名为 username 的数组。我希望用户在之后给我他/她的 ID 我希望用户输入的名称必须与用户名数组中的数组编号相同 例如,如果用户键入 5,那么他/她的名字必须是“f”,否则用户无法继续。

我不知道在 if 语句中输入什么?

class Program
{
    static void Main(string[] args)
    {
        string[] userid = {"0" , "1" , "2" , "3" , "4" , "5"};

        string[] username = { "a" , "b" , "c" , "d" , "e" , "f"};

        Console.Write("please type user id: \t");

        string useridreply= Console.ReadLine();

        Console.Write("please type user name: \t");

        string usernamereply = Console.ReadLine();

        if (usernamereply == username[useridreply])
        {
        }
    } 
}

【问题讨论】:

    标签: c# arrays string if-statement


    【解决方案1】:

    在您的 if 条件中,您忘记再添加一个“=”。
    在 C# 中,比较两个值的语法是“==”。

    所以你的 if 条件看起来像:

    if (usernamereply == username[useridreply])
    

    这是比较运算符的 Microsoft 文档的另一个有用链接:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/comparison-operators

    【讨论】:

    • 谢谢回复:
    • 我按照你说的做了,但仍然在 if 语句中的 useridreply 下面有一条红线,上面写着:无法将类型“字符串”隐式转换为“整数”
    • 老实说,我刚刚意识到我的愚蠢错误,只是我忘记了我在数组 [] 中输入了一个字符串,这就是它导致这个问题的原因。我刚刚将我的 useridreply 更改为 int,现在它可以正常工作了。但还是非常感谢。
    • @Shazil 不客气! :)
    【解决方案2】:

    这是另一种方法。 在这种方法中:

    A. 检查输入的 userid 以验证它存在于数组中。我们可以为此使用 for 循环,但 C# 提供了一个函数。

    B. 只有当 userid 有效时,我们才要求提供用户名。这不会发生在在线系统中,但这取决于您。

    C. 测试时总是使用“==”。一个“=”表示赋值。很常见的错误。

    D、常用变量名如:userName或UserName,当变量名有多个单词时,至少将第二个单词的第一个字母大写,以便于阅读。我没有完全遵循这条规则,我按原样使用了你的一些代码,因为我现在很懒:)

    using System;
    public class Program
    {
        public static void Main()
        {
            string[] userid = {"0" , "1" , "2" , "3" , "4" , "5"};
    
            string[] username = { "a" , "b" , "c" , "d" , "e" , "f"};
    
            Console.Write("please type user id: 	");
    
            string userIdReply= Console.ReadLine();
            //Check if typed userid is in the array or not.
            //Note: "001" is not equal to "1" in this test.
            
            int index = Array.IndexOf(userid, userIdReply);
            if (index == -1)
            {
                    Console.WriteLine("Error-1:Typed userid not found!");
                    return;
            }
            
            //Now we have a valid userid let's get the user name
    
            Console.Write("please type user name: 	");
            string userNameReply = Console.ReadLine();
            
            //Note 1-This test is case sensitive.
            //user name "A" will not match "a".
            //Note 2-C# arrays begin at ZERO not 1.
            //if the userid is 4 the corresponding value will be "e" not "d".
            if (userNameReply != username[index])
            {
                Console.WriteLine("Error-2:Typed userid found but username does not match it-Check the case!");
                return;
            }
        }
    }
    

    【讨论】:

    • 谢谢。我会记住的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2016-12-15
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多