-1

I have a program that I created, the result is not appropriate, I want to do multiplication between rows of numbers -1, -1, -1 and 1,2, -1

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

then the results are summed

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

please solution, for multiplication min (-)

enter image description here

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
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
MHD
  • 1
  • 2

1 Answers1

0

It's not clear what you expect the user to enter in TextBox1, you seem to expect each number to be only one character, but you example includes numbers with more than one character (such as -1).

The following code expects the user to enter three integers separated by spaces in TextBox1. Note that it might make more sense to have a separate TextBox for each number.

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
Blackwood
  • 4,504
  • 16
  • 32
  • 41