努力使童谣条件奏效

时间:2014-10-07 15:54:16

标签: vb.net visual-studio-2010 visual-studio-2012

我一直在用VB制作Baa Baa Black Sheep并且已经陷入了程序的最后一点。我试图让程序说明用户是否为拥有行李的人输入了正确的信息,但它似乎没有注册最后一部分。非常感谢任何帮助!

Module Module1

    Sub Main()
        Dim WoolAnswer As String = ""
        Dim BagNumber As Integer = 0
        Dim FirstBag As String = ""
        Dim SecondBag As String = ""
        Dim ThirdBag As String = ""

        Console.WriteLine("Do you have any wool?")
        WoolAnswer = Console.ReadLine

        If WoolAnswer = "yes" Then
            Console.WriteLine("How many bags do you have?")
            BagNumber = Console.ReadLine

            If BagNumber = 3 Then
                Console.WriteLine("Who is the first bag for?")
                FirstBag = Console.ReadLine()

                Console.WriteLine("Who is the second bag for?")
                SecondBag = Console.ReadLine

                Console.WriteLine("Who is the third bag for?")
                ThirdBag = Console.ReadLine
            Else
                Console.WriteLine("That is not the correct amount of bags.")
            End If

        Else
            Console.WriteLine("You have no wool.")
        End If

        **If (FirstBag = "master" & SecondBag = "dame" & ThirdBag = "little girl") Then
            Console.WriteLine("You really know your nursery rhymes!")
        End If**
        **This is the part that doesn't work**

        Console.ReadLine()
    End Sub

End Module

1 个答案:

答案 0 :(得分:2)

您应该使用AndAlso运算符来比较您的值。

If FirstBag = "master" AndAlso SecondBag = "dame" AndAlso ThirdBag = "little girl" Then

您可以使用普通And运算符执行此操作,但AndAlso支持短路。

编辑:短路是一种编程结构,如果语句的前一部分呈现检查语句的其余部分是无意义的,则允许您跳过对多部分条件语句部分的评估。

示例:如果a == b AndAlso c == d返回c == d,则a == b将不会尝试评估false