【问题标题】:Struggling to make nursery rhyme conditions work努力使童谣条件发挥作用
【发布时间】:2014-10-07 15:54:15
【问题描述】:

我一直在用 VB 制作咩咩黑羊,但一直卡在程序的最后一点。我试图让程序说明用户是否为拥有这些包的人输入了正确的信息,但它似乎没有注册最后一部分。非常感谢任何帮助!

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

【问题讨论】:

  • 你会得到我在 SO 上见过的最奇怪的问题标题的分数。
  • 这不是写 IF 语句的方法;尝试If (FirstBag = "master" AndAlso SecondBag = "dame" AndAlso ThirdBag = "little girl") Then & 符号用于字符串连接
  • 玫瑰是红色,紫罗兰是蓝色,& 用于连接;也给你。

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


【解决方案1】:

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

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

您可以使用普通的 And 运算符来做到这一点,但 AndAlso 支持短路。

编辑:短路是一种编程结构,如果语句的较早部分导致检查语句的其余部分毫无意义,它允许您跳过对多部分条件语句部分的评估。

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

【讨论】:

  • 描述 short circuiting 以获得支持; OP 显然对这两种形式都不太了解
  • 完美!非常感谢:)
  • @Plutonix Welp,你找到我了。
猜你喜欢
  • 2012-02-18
  • 2022-01-18
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
相关资源
最近更新 更多