VB.Net嵌套If

VB.Net嵌套If,vb.net,if-statement,logic,Vb.net,If Statement,Logic,我有一些逻辑和确定的条件,如果没有达到。我已经尝试了if/ifelse/else的几种变体来创建我想要的逻辑,但没有一种能够正确工作。让我给你看一些代码 If(a is true) Then print("A is true") Else If(b is true) Then print("B is true") if(c is true) Then print("B and C are true") else 'c is Not true print("B

我有一些逻辑和确定的条件,如果没有达到。我已经尝试了if/ifelse/else的几种变体来创建我想要的逻辑,但没有一种能够正确工作。让我给你看一些代码

If(a is true) Then
print("A is true")

Else If(b is true) Then
print("B is true")

    if(c is true) Then
    print("B and C are true")

    else 'c is Not true
    print("B is true, C is Not true)

        if(d is true) Then
        print("B and D are true")

        else 'd is Not true
        print("B is true, D is Not true")

        End If
   End If
End If
发生的事情是我的

“If(d为真)”和“else'd为假”

不检查条件。这部分逻辑正在被“超越”

A、B、C和D为真时的预期输出:

“A是真的”

B、C和D为真时的预期输出:

“B是真的”

“B和C是正确的”

B、C和D为真

B和C为真但D为非真时的预期输出:

“B是真的”

“B和C是正确的”

B是真的,D不是真的

B和D为真时的预期结果:

“B是真的”

B是真的,C不是真的

B和D都是真的

我现在看到的是:

B、 C和D是正确的:

“B是真的”

“B和C是正确的”

这就省去了“B和D都是真的”


希望这些结果能帮助你理解

我不确定a、b、c或d是什么,但以下是我认为您正在尝试做的。仅当“d”根据“用例”为true或false时,才需要更改字符串。如果您想要更高的粒度,那么我建议使用前面提到的字符串生成器。无论如何,下面是我用来创建您的“case”的代码,清楚地表明,如果“b”为假,“c”和“d”永远不会被检查。我是根据你的“用例”陈述的


遵循你的逻辑。只有在B为真时才会检查C。同样,只有当B为真而C为非真时,D才会被检查。你到底想要什么样的产出?我认为,在这种情况下,最好分别检查A、B、C和D,并将字符串连接在一起。谢谢您指出这一点。我已经开始编辑,以包含我期望的/预期的结果。我喜欢你的建议,但我只想先测试C和D是否为真。如果A是真的,我不在乎其他任何事情。对不起,原稿很混乱。希望这现在是有意义的。事实上,这些陈述中的一些正在被重复。最后,我使用了一些逻辑,将最后编写的表达式放在一个表中,忽略前面的表达式
If()
是一种在一行中检查和返回对象的方法。正确的方法是:
如果a是真的,那么
对于ElseIf:
ElseIf b是真的
。对不起,我应该明确指出,a、b、c和d都是布尔函数,如果参数为“x”,则返回True或false。对不起,我把它弄糊涂了。您的代码看起来不错,但我确实需要打印语句“B为真,C为非真”。换句话说,我确实需要在“If C Then”进行编辑后使用else语句,这同样不是最佳的,但满足了您的要求
 Private Sub Test()
            Dim a = True
            Dim b = False
            Dim c = True
            Dim d = True

            Dim printout As String = ""
            If a Then
                printout = "a is true"
            Else
                If b Then
                    If c Then
                        printout = "b and c are true"
                    else
                      printout="b is true and c is not true"

                    End If
                    If d and c Then
                        printout = "b and c and d are true"

                    elseif d=true and c=false then

                        printout = "b and d are true , c is not true"
                    elseif c=true and  d=false then
                     printout = "b and c is true ,d is not true"  
                    else
                     printout = "b is true ,c and d are not true"              


                    End If
                End If
            End If
            Console.WriteLine(printout)

        End Sub