VB.NET 中的输入框到数组

发布于 2024-10-18 20:33:02 字数 886 浏览 4 评论 0原文

我这里有一个程序可以解决表达式...

首先我需要在文本框中输入表达式。它将存储在 CharArray 中,然后使用输入框将变量替换为整数...

我的问题是:如何将整数存储到数组中并存储操作?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim arr() As Char = TextBox1.Text.ToCharArray
    Dim aChar As Char
    Dim a As String
    Dim calc() as String

    'Me.Height = Me.Height + 90
    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")

            'Here what code?
            '    Try
            '        calc(i) = a

            '    Catch ex As Exception
            '        MsgBox("eee")
            '    End Try
            'Else
        End If
    Next i
End Sub

I have a program here that will solve an expression...

First I need to input the expression in a textbox. That will be stored in a CharArray and then substitute the variables to integer by using input boxes...

My problem is: How can I store the integer to an array and also store the operation?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim arr() As Char = TextBox1.Text.ToCharArray
    Dim aChar As Char
    Dim a As String
    Dim calc() as String

    'Me.Height = Me.Height + 90
    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")

            'Here what code?
            '    Try
            '        calc(i) = a

            '    Catch ex As Exception
            '        MsgBox("eee")
            '    End Try
            'Else
        End If
    Next i
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潜移默化 2024-10-25 20:33:02

我通常更喜欢使用通用列表,它们使添加和删除项目变得更加容易。下面的代码应该可以满足您的需要:

    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()

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()
三生路 2024-10-25 20:33:02

我还没有测试过这个或任何东西,但我认为稍微重构一下它以使用 For Each 循环,并且确定范围或摆脱 a 和 aChar 变量将是一个更好的方法:

    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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文