使用 If Then Else If 根据首字母选择姓氏,然后显示 MessageBox [英] Using If Then Else If to select Last Name based on First Letter then Display a MessageBox
问题描述
我正在学习介绍.这学期编程,我们用的是VB.我被我的一部分编码难住了.当我单击处理"按钮时,我需要一条消息来显示您在第 3 行.您的密码是 *."
I'm taking Intro. to Programming this semester and we are using VB. I'm stumped on a part of my coding. When I click the "process" button, I need a Message to display "You are in line 3. Your pin number is *."
PIN 码必须由名字的第一个字母、中间名的第一个字母、姓氏的第一个字母和 ID 的前 2 位数字组成.
The pin number must be made of first letter of first name, first letter of middle name, first letter of last name, and first 2 digits of id.
我就是这样开始的,但我知道我要放弃一些东西.条目不应区分大小写.当我只输入一个字母 ex 时.姓氏中的a"它给了我结果例如.第 1 行",但是当我输入一个完整的姓氏时.艾伦"它什么也没给我.
This is how I've started, but I know I'm leaving something off. Entries should not be case sensitive. When I type just a letter ex. "a" in last name it gives me the result ex. "line 1", but when I type a full last name ex. "allen" it gives me nothing.
'使用 if then 判断哪一行消息
'Use if then to determine which line message
If strLastName.Substring(0) = "a-c" Then
strMessage = "line 1."
ElseIf strLastName.Substring(0) = "g-l" Then
strMessage = "line 2."
ElseIf strLastName.Substring(0) = "m-q" Then
strMessage = "line 3."
ElseIf strLastName.Substring(0) = " r-v" Then
strMessage = "line 4."
ElseIf strLastName.Substring(0) = "w-z" Then
strMessage = "line 5."
End If
MessageBox.Show("You are in " & strMessage & " "Your pin number is " & )
End Sub
推荐答案
If
不是这样工作的.strLastName.Substring(0) = "a-c"
将测试第一个字符是否等于a-c",而这永远不可能.甚至a"都不应该工作.这可能不适用于外国字符集,但它是您尝试执行的操作的紧凑形式:
If
doesnt work like that. strLastName.Substring(0) = "a-c"
will be testing if the first character equals "a-c" which it never can be. Not even "a" should work. This likely wont work on foreign character sets, but is a compact form of what you are trying to do:
Select Case strLastName.ToUpper.Substring(0, 1)
Case "A" ,"B", "C" ' test range
strMsg = "line 1"
Case "D" to "G" ' forgot these
strMsg = "line 2"
使用范围作为问题中的代码似乎需要,可以工作,但指定每个字符更好.或者,您可以测试第一个字符是否在表示范围的字符串中:
Using a range as the code in the questions seems to want, can work but specifying each character is better. Alternatively, you could test if the first char is in a string representing a range:
' read the char array of the string for the first letter
Dim ch As String = strLastName(0)
If "ABC".Contains(ch.ToUpper) Then
ElseIf "DEFG".Contains(ch.ToUpper) Then
'...
这篇关于使用 If Then Else If 根据首字母选择姓氏,然后显示 MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!