Arrays 如何检查两个字符串数组是否相等?
我正试图为不同的动物做一个分类程序。下面是我的代码(以一只动物为例)。该程序旨在接收用户对每个问题的答案的输入,然后将其添加到一个数组(answerList)。在本例中,如果Arrays 如何检查两个字符串数组是否相等?,arrays,vb.net,classification,Arrays,Vb.net,Classification,我正试图为不同的动物做一个分类程序。下面是我的代码(以一只动物为例)。该程序旨在接收用户对每个问题的答案的输入,然后将其添加到一个数组(answerList)。在本例中,如果answerList(包含所有用户输入的数组)等于Horse数组,用户将被告知他们的动物是马 但是,代码的最后四行不起作用。它不产生最终输出。有没有办法让我下面的代码正常工作 Sub Main() Dim aAquatic, aInsect, aTentacles, aBird, aFlippers, aFly, a
answerList
(包含所有用户输入的数组)等于Horse
数组,用户将被告知他们的动物是马
但是,代码的最后四行不起作用。它不产生最终输出。有没有办法让我下面的代码正常工作
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
事实上,我对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
而且我也找不到回答列表的声明:
dim answerList as new List(Of String)
希望我能帮上忙。如果去掉第一个数组中的逗号,可能会有所帮助。如果这两个阵列在那里,则无法真正比较这两个阵列…您好,非常感谢您的快速响应!事实上,我犯了另一个错误,其他人发现这个错误阻止了它的工作,但是你的代码也能工作,所以谢谢你的帮助;它实际上在我的实际代码中,但由于某种原因,我忘了在这里添加它!我现在有了一个解决方案,所以不用担心(:由于数组是IEnumerable的,您可以查看一下,以避免仅仅为了检查目的而创建字符串