如何将列表或数组的内容用作变量

时间:2018-11-08 08:00:31

标签: .net vb.net

我刚开始用VB.Net涂鸦 我需要为自己完成的一项小任务提供一些建议。

我想要的是列表的内容可以用作变量。

我已经做了“登录”的事情”:

Sub Main()
    Dim username As String
    Dim password As Integer
    Dim nope As String = "!!!NO ACCESS!!!"
    Dim ukjent As String = "!!!UNKNOWN USER!!!"

    Console.Write("Enter your name: ")
    username = Console.ReadLine()
    Console.Write("Enter Password: ")
    password = Console.ReadLine()

    If username = "jarvis" And password = 1337 Then
        Console.ForegroundColor = ConsoleColor.Green
        Console.WriteLine("Welcome Jarvis")
        Console.WriteLine("Please enter numerical value")

        Dim X As Decimal = Console.ReadLine()
        Dim y As Decimal = Console.ReadLine()
        Dim z As Decimal = Console.ReadLine()
        Dim i As Decimal

        i = X + y + z

        Console.WriteLine(i)
        Console.WriteLine()

    Else
        Console.ForegroundColor = ConsoleColor.Red
        Console.WriteLine(nope)

    End If
    Console.ReadLine()

如果我想使用输入了更多“用户名”和更多“密码”的列表,该怎么办?

我可以这样吗?

Dim username() As String ={"User1","User2"}
Dim password() As Integer ={ 123, 321}

我如何回忆列表中的值? 我知道现在我不考虑将user1与密码123匹配。但这可能会在稍后阶段尝试逐段构建。

1 个答案:

答案 0 :(得分:0)

您不希望您的用户知道用户名或密码或两者都不对;只是说“入场费”无效。不要帮助黑客。

TryParse接受用户输入的字符串。是的,即使输入了数字,它也是一个字符串。查找Console.ReadLine,您将看到它返回一个字符串。

TryParse将测试字符串以查看它是否可以转换为整数。如果成功,它将把该值放到password变量中(在本例中),如果失败,将在密码中放置一个零。

Dictionary的.Keys方法返回所有键的集合。我们使用Contains方法检查输入的用户名是否存在。如果是,我们使用密钥用户名来检索与其关联的值。如果用户名不存在,我们将淘汰。

最后,我们对照输入的密码检查了存储的密码。

所有这些检查是因为我们不能信任用户输入我们希望他们输入的内容。

Sub Main()
        Dim X, Y, Z As Decimal
        Dim StoredPassword As Integer 'refers to what is in the Dictionary
        Dim username As String = ""
        Dim password As Integer
        Dim nope As String = "!!!NO ACCESS!!!"
        Dim Users As New Dictionary(Of String, Integer) From {{"Mathew", 123}, {"Mark", 456}, {"Luke", 321}}
        Console.Write("Enter your name: ")
        username = Console.ReadLine()
        Console.Write("Enter Password: ")

        Integer.TryParse(Console.ReadLine(), password)
        If Users.Keys.Contains(username) Then
            StoredPassword = Users(username)
        Else
            Console.ForegroundColor = ConsoleColor.Red
            Console.WriteLine(nope)
            Console.ReadLine()
            End
        End If
        If StoredPassword = password Then
            Console.ForegroundColor = ConsoleColor.Green
            Console.WriteLine("Successful Login")
            Console.WriteLine($"Welcome {username}!")
            Console.ForegroundColor = ConsoleColor.White
        Else
            Console.ForegroundColor = ConsoleColor.Red
            Console.WriteLine(nope)
            Console.ReadLine()
            End
        End If

        Console.WriteLine("Please enter numerical value")
        Decimal.TryParse(Console.ReadLine(), X)
        Console.WriteLine("Please enter numerical value")
        Decimal.TryParse(Console.ReadLine(), Y)
        Console.WriteLine("Please enter numerical value")
        Decimal.TryParse(Console.ReadLine(), Z)
        Dim i As Decimal

        i = X + Y + Z

        Console.WriteLine($"The sum is {i}")

        Console.ReadLine() 'keeps the console window open
End Sub