2015-09-11 50 views
-1

我有一些逻辑和一定条件没有达到。我尝试了if/ifelse/else的几种变体来创建我想要的逻辑,但没有任何正确的工作。让我告诉你一些代码...VB.Net嵌套如果

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 

正在发生的事情是,我

“如果(d为真)”和“其他“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是真正的’

希望这些成果有助于UND erstand!

+1

按照你的逻辑。如果B是真的,C只会被检查。同样,如果B为真且C不正确,D将只会被检查。你究竟在寻找什么样的产出?我认为,在这种情况下,最好分别检查A,B,C和D中的每一个,并将字符串连接在一起。 – RianBattle

+0

谢谢你指出这一点。我已经进行了编辑,以包括我期望的/预期的结果。我喜欢你的建议,但我只想测试C和D,如果B是真的第一。如果A是真的,我不关心其他事情。对不起原来很混乱。希望这现在有道理。 – Christopher

+0

其中一些陈述正在重复的事实很好。最后,我使用了一些逻辑来放置最终表达式,并将它放在表中并忽略早先的表达式。 – Christopher

回答

1

我不确定a,b,c或d是什么,但这是我认为你正在尝试做的。您只需要根据您的“用例”在“d”为true或false时更改该字符串。如果你想要更多的粒度,那么我会建议一个字符串生成器,如前所述。无论如何,这里是我用来创建你的“案例”的代码要清楚,如果“b”为假,从不检查“c”和“d”。我基于你的“用例”所陈述的内容。

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 
+0

对不起,我应该明确指出,a,b,c和d都是布尔函数,它们返回true或false,提供参数“x”。我非常困惑,我很抱歉。你的代码看起来不错,但是我确实需要打印语句“”B是真的,C是不真实的。“换句话说,我真的需要在”If c Then“之后有一个else语句 – Christopher

+0

进行编辑,但这不是最佳但完成你所问 –