Frage

Ich habe hier ein Programm, das einen Ausdruck lösen wird ...

Zuerst muss ich den Ausdruck in ein Textfeld eingeben.Das wird in einem Chararray aufbewahrt und ersetzen Sie die Variablen anschließend mit den Eingabefeldern ...

Mein Problem ist: Wie kann ich die Ganzzahl an einem Array speichern und auch den Betrieb speichern? generasacodicetagpre.

War es hilfreich?

Lösung

I generally prefer to work with generic Lists, they make adding and removing items much easier. The code below should do what you need:

    Dim arr() As Char = TextBox1.Text.ToCharArray
    Dim aChar As Char
    Dim a As String
    Dim calc As New List(Of String)

    Me.Button1.Enabled = False
    Me.TextBox1.Enabled = False
    For i = 0 To TextBox1.Text.Length() - 1
        aChar = arr.ElementAt(i)
        If Char.IsLetter(aChar) Then
            a = InputBox("Enter value for " & aChar, "Expression")
            ''//Add the result to the list
            calc.Add(a)
        Else
            ''//Add the operator to the list
            calc.Add(aChar)
        End If
    Next i

    ''//If you want to convert to list to an array you can use this
    Dim CalcArray = calc.ToArray()

Andere Tipps

I haven't tested this or anything, but I think refactoring this a tad to use the For Each loop and either scoping or get rid of the a and aChar variables would be a little nicer approach:

    Dim arr() As Char = TextBox1.Text.ToCharArray
    Dim calc As New List(Of String)

    Me.Button1.Enabled = False
    Me.TextBox1.Enabled = False

    For Each aChar As Char In arr
        If Char.IsLetter(aChar) Then
            ''//Add the result to the list
            calc.Add(InputBox(String.Format("Enter value for {0} Expression", aChar.ToString)))
        Else
            ''//Add the operator to the list
            calc.Add(aChar.ToString)
        End If
    Next

    ''//If you want to convert to list to an array you can use this
    Dim CalcArray = calc.ToArray()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top