首页 > 解决方案 > 如何使用列表或数组的内容作为变量

问题描述

我刚刚开始用 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 匹配。但这可能会在稍后阶段出现,尝试逐个构建。

标签: .netvb.net

解决方案


您不希望您的用户知道是用户名还是密码或两者都错误;只是长期无效。不要帮助黑客。

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

TryParse 将测试字符串以查看它是否可以转换为整数。如果成功,它将将该值放入密码变量(在此示例中),如果失败,则将零放入密码中。

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

推荐阅读