繁体   English   中英

如何在 VB.NET 中创建多重 if 语句?

[英]How do I create a multiple if statement in VB.NET?

所以我试图通过将表达式推入堆栈来制作一个中缀来反转波兰符号程序。 该程序迭代表达式以查找运算符,一旦找到它,它就会弹出 2 个值并执行计算。 但是,如果我尝试添加多个 if 语句,则会弹出转换错误。 “if express(i) <> “+” 行用于添加但是如果我要通过添加多个条件来扩展它“if express(i) <> “+” or “-” or “*” 然后它说“从字符串“-”转换为类型 Boolean 无效。 谁能帮我解决这个问题? 谢谢。

模块模块1

Sub Main()
    Dim expres As String
    Console.WriteLine("Enter infix expression")
    expres = Console.ReadLine()
    Dim S As New Stack
    Dim current(1) As Integer
    Dim temp_val As Integer
    For i = 0 To expres.Length - 1
        If expres(i) <> "+" Then
            S.Push(expres(i))
        End If

        If expres(i) = "+" Then
            current(0) = S.Pop().ToString
            current(1) = S.Pop().ToString
            temp_val = current(0) + current(1)
            Console.WriteLine(temp_val)
            S.Push(temp_val)

        End If
        If expres(i) = "-" Then
            current(0) = S.Pop().ToString
            current(1) = S.Pop().ToString
            temp_val = current(0) - current(1)
            Console.WriteLine(temp_val)
            S.Push(temp_val)

        End If
        If expres(i) = "*" Then
            current(0) = S.Pop().ToString
            current(1) = S.Pop().ToString
            temp_val = current(0) * current(1)
            Console.WriteLine(temp_val)
            S.Push(temp_val)

        End If
        If expres(i) = "/" Then
            current(0) = S.Pop().ToString
            current(1) = S.Pop().ToString
            temp_val = current(0) / current(1)
            Console.WriteLine(temp_val)
            S.Push(temp_val)

        End If
        If expres(i) = "^" Then
            current(0) = S.Pop().ToString
            current(1) = S.Pop().ToString
            temp_val = current(0) ^ current(1)
            Console.WriteLine(temp_val)
            S.Push(temp_val)

        End If
        If expres(i) = "~" Then
            current(0) = S.Pop().ToString
            current(1) = S.Pop().ToString
            temp_val = current(0) + current(1)
            Console.WriteLine(temp_val)
            S.Push(temp_val)

        End If
    Next
    Console.ReadLine()
End Sub

端模块

每次比较时,您应该 state 左表达式,例如

if express(i) <> "+" or express(i) <> "-" or express(i) <> "*"

试试这个

Module Module1

    Sub Main()
        Dim expres As String
        Console.WriteLine("Enter infix expression")
        expres = Console.ReadLine()
        Dim s As New Stack
        Dim current(1) As Integer
        Dim tempVal As Integer
        For i = 0 To expres.Length - 1

            If Array.IndexOf({"+", "-", "*", "/", "^", "~"}, expres(i)) = -1 Then
                s.Push(expres(i))
            End If

            current(0) = s.Pop().ToString
            current(1) = s.Pop().ToString

            Select Case expres(i)
                Case "+"
                    tempVal = current(0) + current(1)

                Case "-"
                    tempVal = current(0) - current(1)

                Case "*"
                    tempVal = current(0) * current(1)


                Case "/"
                    tempVal = current(0) / current(1)

                Case "^"
                    tempVal = current(0) ^ current(1)


                Case "~"
                    tempVal = current(0) + current(1)

            End Select
            Console.WriteLine(tempVal)
            s.Push(tempVal)

        Next
        Console.ReadLine()
    End Sub
End Module

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM