Using If Then Else If to select Last Name based on

2019-09-03 19:49发布

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 *."

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.

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.

'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

标签: vb.net
3条回答
Deceive 欺骗
2楼-- · 2019-09-03 20:00

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
'...
查看更多
狗以群分
3楼-- · 2019-09-03 20:16

Long story short: convert to ASCII, then use Select-Case ranges.

'Convert to lowercase or uppercase.  I chose lowercase; just keep it constant.
Dim ascii As Integer = AscW(strLastName.Substring(0).ToLower())
Select Case ascii
    Case 97 To 99 'ASCII a to ASCII c
        strMessage = "line 1."
    Case 100 To 108 'ASCII d to ASCII l -- assuming your g was supposed to be a d
        strMessage = "line 2."
    Case 109 To 113 'ASCII m to ASCII q
        strMessage = "line 3."
    Case 114 To 118 'ASCII r to ASCII v
        strMessage = "line 4."
    Case 119 To 122 'ASCII w to ASCII z
        strMessage = "line 5."
    Case Else
        'Non-alphabetical first character
End Select


MessageBox.Show("You are in " & strMessage & " Your pin number is " & intPin)
查看更多
迷人小祖宗
4楼-- · 2019-09-03 20:19

String.Substring when called with only one integer parameter means, take everything from the index passed to the end of the string. So, if the strLastName is "abc" with strLastName.Substring(0) you get back "abc"

To get the character at the first position in strLastName you write

Dim ch = strLastName(0)

Now to check in which line (?) you are, you could write something like this

Sub Main
    Dim strLastName = "abc"
    Dim ch = strLastName(0)

    Dim line1 as Char() = new Char() {"a","b", "c"}
    Dim line2 as Char() = new Char() {"g","h", "l"}
    Dim line3 as Char() = new Char() {"m","o", "p", "q"}
    Dim line4 as Char() = new Char() {"g","h", "l"}
    Dim line5 as Char() = new Char() {"r","u", "v"}

    if line1.Contains(ch) then
        Console.WriteLine("Line 1")
    Else if line2.Contains(ch) then
        Console.WriteLine("Line 2")
    Else if line3.Contains(ch) then
        Console.WriteLine("Line 3")
    Else if line4.Contains(ch) then
        Console.WriteLine("Line 4")
    Else if line5.Contains(ch) then
        Console.WriteLine("Line 5")
    Else 
        Console.WriteLine("Unexpected char found " & ch)
    End if 
End Sub

Of course the arrays above are defined with arbitrary elements and with only the lower case versions of the letters. But this is just for example.

查看更多
登录 后发表回答