2

这真的让我很困惑。我正在尝试做一个 Visual Basic 控制台应用程序,如果用户输入“A”或“a”,那么程序应该执行“x”,但这不起作用。我得到的错误是:

从字符串“a”到类型“Boolean”的转换无效。

这是我的代码:

模块模块1

Sub Main()
    Dim Selection As String

    Console.WriteLine("Please select your function:")
    Console.WriteLine("* To convert binary to decimal, press A,")
    Console.WriteLine("* Or to convert decimal to binary, press B")

    Selection = Console.ReadLine

    If Selection = "A" Or "a" Then
        Console.WriteLine("This will be A")
    ElseIf Selection = "B" Or "b" Then
        Console.WriteLine("This will be B")
    ElseIf Selection = Not "A" Or "a" Or "B" Or "b" Then
        Console.WriteLine("Please try again")
        Do Until Selection = "A" Or "a" Or "B" Or "b"
        Loop
    End If


End Sub

应该在这段代码中正确使用 Or 以使其正常运行?

谢谢,

杰克

4

4 回答 4

5

你的If条款应该是这样的:

If Selection = "A" Or Selection = "a" Then

之间的子句Or应该是布尔表达式,并且只是一个字符,而不是布尔表达式。"a"

于 2012-10-29T20:04:08.337 回答
3

使用Select Case是另一种选择:

Sub Main()
    Console.WriteLine("Please select your function:")
    Console.WriteLine("* To convert binary to decimal, press A,")
    Console.WriteLine("* Or to convert decimal to binary, press B")
    Console.WriteLine("* To Quit, press Q")
    Dim Selection As String = ValidateInput()
    If Selection <> "Q" Then
        'do something
    End If
End Sub

Function ValidateInput() As String

    Dim Selection As String
    Selection = Console.ReadLine

    Select Case Selection.ToUpper
        Case "A"
            Console.WriteLine("This will be A")
        Case "B"
            Console.WriteLine("This will be B")
        Case "Q"
            Return "Q"
        Case Else
            Console.WriteLine("Please try again")
            ValidateInput()
    End Select
    Return Selection

End Function
于 2012-10-29T20:27:13.223 回答
3

我建议在 if 语句之前将输入的字符串更改为大写。最后一个 else if 语句也不需要,可以用一个 else 代替。

Sub Main()
    Dim Selection As String

    Console.WriteLine("Please select your function:")
    Console.WriteLine("* To convert binary to decimal, press A,")
    Console.WriteLine("* Or to convert decimal to binary, press B")

    Selection = Console.ReadLine.ToUpper() 'Not tested otherwise use the one below
'Selection=Selection.ToUpper()

    If Selection = "A" Then
        Console.WriteLine("This will be A")
    ElseIf Selection = "B"  Then
        Console.WriteLine("This will be B")
    Else
        Do Until Selection = "A"  Or Selection = "B" 
               'Loop Code goes here
        Loop
    End If


End Sub
于 2012-10-29T20:11:36.287 回答
0

Estou tentando fazer um sistema de gerenciamento de uma loja de cafe, mas o Login não ta dando certo, quem puder me ajudar por赞成 eu agradeço。

O código com o erro é ess:

Public Class Login
      
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If UnameTb.Text = "" Or PasswordTb.Text Then
        MsgBox("Digite o Nome de Usuário e a Senha")
    ElseIf UnameTb.Text = "Admin" And PasswordTb.Text = "Password" Then
        Dim Obj = New Items
        Obj.Show()
        Me.Hide()
    Else
        MsgBox("Nome de Usuário ou Senha Incorretos")
        UnameTb.Text = ""
        PasswordTb.Text = ""

    End If
End Sub
于 2021-09-18T20:31:54.063 回答