-1

我有一个我创建的程序,结果不合适,我想在数字行 -1、-1、-1 和 1,2、-1 之间进行乘法运算

-1x1 = -1
-1x2 = -2
-1x-1 = 1

然后将结果相加

(-1) + (- 2) + 1 = -2

请解决,乘法分钟( - )

在此处输入图像描述

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click

    Dim Total, Total2 As Integer
    Dim y As Integer = 0
    Dim b As Integer = 2
    Dim getArray As String = TextBox3.Text
    Dim arrVin() As Char = getArray.ToCharArray
    'Dim getArray2 As String = TextBox2.Text
    'Dim arrVin2() As Char = getArray2.ToCharArray
    Dim multArr() As Integer = {1, 2, -1}
    'Dim m As Integer = 1
    'Total = 0
    For Each x As String In arrVin
        If IsNumeric(x) = True Then
            'For Each i As String In arrVin2
            'muliply by a bigger number each time
            'm = 2

            'add the new product to the running total
            'Total = Total + (Val(x) + Val(x))
            'Total2 = (Val(x) * Val(i)) + Total2
            'Total = Total + (m * Val(x))
            Total2 += x * multArr(y)
            y = y + 1
            TextBox1.Text = Total2 + b
        End If
    Next
End Sub
4

1 回答 1

0

目前尚不清楚您希望用户在 TextBox1 中输入什么,您似乎希望每个数字只有一个字符,但您的示例包括具有多个字符的数字(例如 -1)。

下面的代码要求用户在 TextBox1 中输入三个用空格分隔的整数。请注意,为每个数字设置一个单独的 TextBox 可能更有意义。

Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    Dim multArray() As Integer = {-1,-1,-1}
    Dim dataStr() As String = TextBox1.Text.Split({" "c},StringSplitOptions.RemoveEmptyEntries)
    If dataStr.Length <> multArray.Length Then 
        MessageBox.Show("Please enter 3 numbers separated by spaces")
        Exit Sub 
    End If
    Dim total As Integer  
    For i As Integer = 0 To multArray.Length - 1
        Dim num As Integer 
        If Not Integer.TryParse(dataStr(i), num) Then
            MessageBox.Show("Please enter valid integers")
            Exit Sub 
        End If
    total += multArray(i) * num
    Next  
    Label4.Text = total.ToString 
End Sub
于 2015-06-08T11:55:26.050 回答