【问题标题】:Conversion from string "a" to type 'Boolean' is not valid从字符串“a”到类型“布尔”的转换无效
【发布时间】:2012-10-19 04:30:48
【问题描述】:

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

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

这是我的代码:

模块模块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 以使其正常运行?

谢谢,

杰克

【问题讨论】:

  • 您还应该为用户提供一条逃生路线,例如“输入 Q 退出”。
  • @rontornambe 谢谢,我现在加了一个。

标签: vb.net console-application


【解决方案1】:

你的If 子句应该是这样的:

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

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

【讨论】:

  • 感谢您的精彩回答 - 这真的很有帮助。当允许我这样做时,我会将其标记为答案。另一件事,这是否适用于 Not 语句?就在我这样做时: " ElseIf Selection = Not "A" Or Selection = Not "a" Or Selection = Not "B" Or Selection = Not "b" Then" ,我得到同样的错误。
  • * 从字符串“f”到类型“Double”的转换无效。
  • @JakeElsley - 语法是:ElseIf Not Selection = "A" Or Not Selection = "a" Or Not Selection = "B" Or Not Selection = "b" Then,但您确实需要考虑这意味着什么...您可能应该使用 AND NOT 而不是 OR NOT
  • 应该是或者不是Selection = "B"。 not 必须在变量之前。我同意 Oded 的观点,你必须考虑这意味着什么。您可能希望它是 AND 而不是 OR
  • 感谢两位对此的快速回复。我可以确认 Oded 的代码可以创造奇迹。
【解决方案2】:

我建议在 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

【讨论】:

  • 这听起来像是解决用户输入的另一种很好的方法,谢谢。这也减少了代码。
【解决方案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

【讨论】:

  • 哇,谢谢 - 我在这里学到的东西比老师教我的还多!谢谢!使用函数是另一种实现方式,理论上这会使我的代码更有条理,而不是在有意义的情况下使用一大堆代码。
  • Select case 是一个更好的选择。在看到这个答案之前,我打算自己发布一个。 +1
  • 感谢您的好心 cmets Jake 和 Neolisk。很高兴能为您提供帮助。
【解决方案4】:

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。

哦,我错了:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2013-10-31
    • 2011-07-30
    • 2015-02-09
    相关资源
    最近更新 更多