从字符串“ a”的转换键入“布尔”无效 [英] Conversion from string "a" to type 'Boolean' is not valid

查看:89
本文介绍了从字符串“ a”的转换键入“布尔”无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的让我感到困惑。我正在尝试创建一个可视的基本控制台应用程序,如果用户输入 A或 a,则该程序应输入 x,但这是行不通的。我得到的错误是:

This one's really got me confused. I'm trying to do a visual basic console application that if the user enters 'A' or 'a' then the program should do 'x', but that's not working. The error I get is:

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

这是我的代码:

模块Module1

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的正确用法是什么在这段代码中使其正常运行?

What should be the correct usage of the Or in this piece of code to make it function correctly?

谢谢

Jake

推荐答案

您的 If 子句应类似于:

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

Or 应该每个是布尔表达式,而 a 只是一个字符,而不是布尔表达式。

The clauses between the Or should each be boolean expressions, and "a" is simply a character, not a boolean expression.

这篇关于从字符串“ a”的转换键入“布尔”无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆