如何检查两个字符串数组是否相等?

时间:2016-10-21 11:15:47

标签: arrays vb.net classification

我正在尝试为不同的动物制作分类程序。下面是我的代码(仅以一只动物为例)。该程序旨在接收用户对每个问题的答案的输入,然后将它们添加到数组(answerList)。在此示例中,如果answerList(包含所有用户输入的数组)等于Horse数组,则会告诉用户他们的动物是马。

但是,最后four lines代码不起作用。它不产生最终输出。反正有没有让我的下面的代码工作?

Sub Main()
    Dim aAquatic, aInsect, aTentacles, aBird, aFlippers, aFly, aHoney, aLegs, aFeline, aStripes, aDomestic, aMilk, aWool, aSnout
    Dim Horse = {“n”, “n”, “n”, “n”, “n,”, "n", “n”, “n”, “n”, “n”, “n”, “n”, “n”, “n”}
    Dim animalArray = {Horse, Cow, Sheep, Pig, Dog, Cat, Lion, Tiger, Dolphin, Seal, Penguin, Ostrich, Sparrow, Bee, Wasp, Termite, Octopus}
    Console.WriteLine("Welcome to the classification. Choose one of these animals and I will attempt to guess it: horse, cow, sheep, pig, dog, cat, lion, tiger, dolphin, seal, penguin, ostrich, sparrow, bee, wasp, termite, octopus. Click enter to continue.")
    Console.ReadLine()
    Console.WriteLine("Is your animal aquatic? (y or n)")
    aAquatic = CStr(Console.ReadLine())
    If aAquatic = "y" Then
        answerList.Add("y")
    ElseIf aAquatic = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Is your animal an insect? (y or n)")
    aInsect = CStr(Console.ReadLine())
    If aInsect = "y" Then
        answerList.Add("y")
    ElseIf aInsect = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Does your animal have tentacles? (y or n)")
    aTentacles = CStr(Console.ReadLine())
    If aTentacles = "y" Then
        answerList.Add("y")
    ElseIf aTentacles = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Is your animal a bird? (y or n)")
    aBird = CStr(Console.ReadLine())
    If aBird = "y" Then
        answerList.Add("y")
    ElseIf aBird = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Does your animal have flippers? (y or n)")
    aFlippers = CStr(Console.ReadLine())
    If aFlippers = "y" Then
        answerList.Add("y")
    ElseIf aFlippers = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Can your animal fly? (y or n)")
    aFly = CStr(Console.ReadLine())
    If aFly = "y" Then
        answerList.Add("y")
    ElseIf aFly = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Can your animal make honey? (y or n)")
    aHoney = CStr(Console.ReadLine())
    If aHoney = "y" Then
        answerList.Add("y")
    ElseIf aHoney = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Does your animal have two legs? (y or n)")
    aLegs = CStr(Console.ReadLine())
    If aLegs = "y" Then
        answerList.Add("y")
    ElseIf aLegs = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Is your animal a feline? (y or n)")
    aFeline = CStr(Console.ReadLine())
    If aFeline = "y" Then
        answerList.Add("y")
    ElseIf aFeline = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Does your animal have stripes? (y or n)")
    aStripes = CStr(Console.ReadLine())
    If aStripes = "y" Then
        answerList.Add("y")
    ElseIf aStripes = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Is your animal a domestic pet? (y or n)")
    aDomestic = CStr(Console.ReadLine())
    If aDomestic = "y" Then
        answerList.Add("y")
    ElseIf aDomestic = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Does your animal produce milk we can drink? (y or n)")
    aMilk = CStr(Console.ReadLine())
    If aMilk = "y" Then
        answerList.Add("y")
    ElseIf aMilk = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Does your animal have wool? (y or n)")
    aWool = CStr(Console.ReadLine())
    If aWool = "y" Then
        answerList.Add("y")
    ElseIf aWool = "n" Then
        answerList.Add("n")
    End If
    Console.WriteLine("Does your animal have a snout? (y or n)")
    aSnout = CStr(Console.ReadLine())
    If aSnout = "y" Then
        answerList.Add("y")
    ElseIf aSnout = "n" Then
        answerList.Add("n")
    End If
    Console.ReadKey()

    String.Join(", ", answerList)
    String.Join(", ", Horse)
    If answerList Is Horse Then
        Console.WriteLine("Your animal is a horse.")
    End If

2 个答案:

答案 0 :(得分:0)

实际上我并不熟悉VB.Net,但不应该是:

dim s1 as String = String.Join(", ", answerList)
dim s2 as String = String.Join(", ", Horse)
If s1 = s2 Then
  Console.WriteLine("Your animal is a horse.")
End If

而且我也找不到answerList的声明:

dim answerList as new List(Of String)

希望我能提供帮助。

答案 1 :(得分:-1)

如果你摆脱了第一个数组中的逗号,可能会有所帮助。如果那里有那个......那就无法比较两个阵列......