2
    Dim keys1() As String = {"corrupt", "selfish", "power", "lying", "lies", "media"}
    Dim terms1 As Integer = 0
    Dim terms1string As String = ""
    terms1string = Console.ReadLine()
    For Each st As String In keys1
        terms1 = terms1 + 1
    Next
    If terms1 < 2 Then
        Console.WriteLine("yay!")
    Else
        Console.WriteLine("YouFail")
    End If

这是我的代码。我希望如果您输入的字符串包含两个以上的术语,那么它会写成“Yay”——否则它会写成“YouFail”。

---更新 8/29/12---

    Function StageTwo(ByVal fname, ByVal lname, ByVal city)
    Console.WriteLine("Describe the U.S. Government.")
    Dim overall As Integer = 0
    Dim keys1() As String = {"corrupt", "selfish", "power", "lying", "lies", "media"}
    Dim terms1 As Integer = 0
    Dim terms1string As String = ""
    terms1string = Console.ReadLine()
    For Each st As String In keys1
        If InStr(terms1string, st) > 0 Then '<<<this line right here!
            terms1 = terms1 + 1
        End If
    Next
    If terms1 < 0 Then
        Console.WriteLine("yay!")
        overall = overall + 1
    End If
    Console.WriteLine()
    Console.WriteLine("Describe the economic status in the U.S.")
    Dim keys2() As String = {"broken", "backed", "failed", "skewed", "tilted", "99%", "rigged", "unfair"}
    Dim terms2 As Integer = 0
    Dim terms2string As String = ""
    terms2string = Console.ReadLine()
    For Each st As String In keys2
        If InStr(terms2string, st) > 0 Then '<<<this line right here!
            terms2 = terms2 + 1
        End If
    Next
    If terms2 < 0 Then
        Console.WriteLine("yay!")
        overall = overall + 1
    End If
    If overall = 2 Then
        Console.WriteLine()
        Console.WriteLine("Enter a username.")
        Dim username As String = ""
        username = Console.ReadLine()
        Console.WriteLine("Please wait.")
        IsURLValid(username, overall)
    Else
        Console.WriteLine("Test Failed.")
    End If
    System.Threading.Thread.Sleep(2000)
End Function

那是我的新代码。仍然无法正常工作,在第一个输入损坏后第二个输入损坏后,它的打印测试失败。再帮忙?非常感谢你们。

4

4 回答 4

2

为什么这么复杂?只需使用Count

Dim keys1() As String = {"corrupt", "selfish", "power", "lying", "lies", "media"}
Dim terms1string = Console.ReadLine()

Dim terms1 = keys1.Count(function(key) terms1string like "*" & key & "*")

If terms1 < 2 Then
    Console.WriteLine("yay!")
Else
    Console.WriteLine("YouFail")
End If

如果要匹配单个单词(foobar power lies是 2 个匹配项,foobarpowerlies是 0 个匹配项),可以改用这一行:

Dim terms1 = keys1.Count(function(key) terms1string.Split().Contains(key))

为了完整起见,这是一个正则表达式版本:

' generous match ('foobarpowerlies' => 2 matches)
Dim pattern = String.Join("|", keys1)
Dim terms1 = Regex.Matches(terms1string, pattern).Count

或者

' strict match using word boundaries ('foobarpowerlies' => 0 matches, but 'foobar power lies' => 2 matches)
Dim pattern = String.Join("|", keys1.Select(function(key) "\b" & key & "\b"))
Dim terms1 = Regex.Matches(terms1string, pattern).Count
于 2012-08-29T07:06:29.767 回答
2

“Austin Powers”应该匹配“power”,“uncorrupt”应该匹配“corrupt”吗?假设“否”
“POWER”应该匹配“power”吗?假设“是”

最安全的方法是使用正则表达式

Function WordCount(keys() As String, terms As String) As Integer
    Dim pattern As String = "\b(" + Regex.Escape(keys(0))
    For Each key In keys.Skip(1)
        pattern += "|" + Regex.Escape(key)
    Next
    pattern += ")\b"

    Return Regex.Matches("terms", pattern, RegexOptions.IgnoreCase).Count
End Function


Sub Main()
    Dim keys1() As String = {"corrupt", "selfish", "power", "lying", "lies", "media"}
    Dim count As Integer
    count = WordCount(keys1, "lying son of a corrupt . . .") ' returns 2
    count = WordCount(keys1, "Never caught lying and uncorrupt . . .") ' returns 1
End Sub

Regex.Escape功能确保您的键中的任何正则表达式特定字符都将被转义,并且不会被视为正则表达式命令。

RegexOptions.IgnoreCase选项告诉它进行不区分大小写的匹配。

\b是一个单词边界,所以在匹配之前和之后必须有一个单词边界(空格、标点符号、换行符、字符串开头、字符串结尾等)。

把键放在这个结构中(key1|key2|key3)表示它可以匹配key1 key2 key3

希望这可以帮助

于 2012-08-29T07:46:53.753 回答
1

我有东西给你。

你父亲的 INSTR()。这是 QuickBasic 4.5 黑客的武器。不像正则表达式那样笨拙或随机;更文明时代的优雅武器。

Module Module1

  Sub Main()
    Dim keys1() As String = {"corrupt", "selfish", "power", "lying", "lies", "media"}
    Dim terms1 As Integer = 0
    Dim terms1string As String = ""
    terms1string = Console.ReadLine()
    For Each st As String In keys1
      If InStr(terms1string, st) > 0 Then '<<<this line right here!
        terms1 = terms1 + 1
      End If
    Next st
    If terms1 < 2 Then
      Console.WriteLine("yay!")
    Else
      Console.WriteLine("YouFail")
    End If
    Console.ReadKey()

  End Sub

End Module
于 2012-08-29T06:34:56.453 回答
0

也许太简单了,但是如果您使用IndexOf,您可以将您的 For 循环更改为:

    If Not String.IsNullOrEmpty(terms1string) Then
        For Each st As String In keys1
            If terms1string.IndexOf(st) <> -1 Then
                terms1 = terms1 + 1
            End If
        Next
    End If

这很简单,因为它不会对输入进行标记......所以像“腐败”和“谎言”这样的词将注册匹配。如果您需要完全匹配,请查看String.Split以获取输入单词,然后有许多算法选项可以将该列表与您的键列表进行比较。

于 2012-08-29T05:22:49.640 回答