从字符串

时间:2018-01-25 00:42:08

标签: vb.net sum numbers

这是我的代码:

Public Class Form1
    Function AnswerQuestion(ByVal s As String) As String
        If s.Contains("How much is") Then
            Dim sum As Integer = 0
            For Each ch As Char In s
                Dim i As Integer
                If Integer.TryParse(ch, i) Then
                    sum += i
                End If
            Next
            Return "The answer is:" & sum.ToString
        Else
            Return Nothing
        End If
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(AnswerQuestion(TextBox1.Text))
    End Sub
End Class

我希望它总结我在“多少就是”之后所做的任何事情,例如,如果我把“多少就是:2 + 8”它返回10作为结果,这是正确的,但如果我把“如何很多是:12 + 5“它返回8而不是17:/我试图做一些研究但是从我尝试过的许多代码中,所有这些代码都给了我相同或最差的结果。有办法吗?

0 个答案:

没有答案