使用If Then Else If如果根据First Letter选择Last Name,则显示MessageBox

时间:2014-06-28 14:48:22

标签: vb.net

我正在参加Intro。本学期编程,我们正在使用VB。我对编码的一部分感到难过。当我点击“处理”按钮时,我需要一条消息来显示“你在第3行。你的密码是 * 。”

密码必须由名字的第一个字母,中间名的第一个字母,姓氏的第一个字母和id的前2个数字组成。

这就是我的开始,但我知道我要离开。参赛作品不应区分大小写。当我只输入一封信时。姓氏中的“a”它给了我结果ex。 “第1行”,但是当我输入一个完整的姓氏ex。 “艾伦”它什么都没给我。

'然后使用以确定哪行消息

    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

3 个答案:

答案 0 :(得分:1)

If不能那样工作。 strLastName.Substring(0) = "a-c"将测试第一个字符是否等于" a-c"它永远不可能。甚至不是" a"应该管用。这可能不适用于外国字符集,但它是您尝试做的紧凑形式:

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"

使用范围作为问题中的代码似乎需要,可以工作,但指定每个字符更好。或者,您可以测试第一个char是否在表示范围的字符串中:

' 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
'...

答案 1 :(得分:0)

长话短说:转换为ASCII,然后使用Select-Case范围。

'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)

答案 2 :(得分:0)

仅使用一个整数参数调用时,

String.Substring表示从索引传递到字符串末尾的所有内容。所以,如果strLastName是" abc"与strLastName.Substring(0)一起回来" abc"

要在strLastName的第一个位置获取字符,请编写

Dim ch = strLastName(0)

现在要检查你是哪一行(?),你可以写这样的东西

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

当然,上面的数组是用任意元素定义的,只有小写字母的版本。但这仅仅是例如。