.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 匹配。但这可能会在稍后阶段出现,尝试逐个构建。
解决方案
您不希望您的用户知道是用户名还是密码或两者都错误;只是长期无效。不要帮助黑客。
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
推荐阅读
- twilio - 使用 Twilio Autopilot API 的任务信心问题
- moodle - Moodle:授予经理用户访问“用户个人资料字段”的权限
- php - 如何使用 Simple HTML Dom Parser 从 HTML 中删除类和 ID 属性
- docker - 重启节点后 Docker swarm 重新创建容器
- function - 将矩阵元素相乘创建一个函数并使用重复结构
- reactjs - 为什么我的 React Native useState 设置不会更新?
- r - 在 Shiny App 中调整大小的图像上的单击坐标不覆盖完整图像
- java - Android java更新所有类中的相同数组对象
- google-sheets - 计算 Google 电子表格中特定单词的出现次数
- angular - Angular TypeScript 最佳实践