Vb.net 如何调整循环并将1添加到整数?

Vb.net 如何调整循环并将1添加到整数?,vb.net,Vb.net,我需要帮助。 可能是循环命令是有用的,但我不知道如何。 对不起,英语不好:P 这里有更多的细节 我不想在VB.Net应用程序中再使用此代码: Dim s As String = TypeHere.Text ' Heres target: TypeHere.Text Dim Words() As String = s.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries) If

我需要帮助。 可能是循环命令是有用的,但我不知道如何。 对不起,英语不好:P 这里有更多的细节

我不想在VB.Net应用程序中再使用此代码:

        Dim s As String = TypeHere.Text ' Heres target: TypeHere.Text
        Dim Words() As String = s.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
        If Words.Length > 0 Then
            c1 = Words(0)
            If Words.Length > 1 Then
                c2 = Words(1)
            End If
            If Words.Length > 2 Then
                c3 = Words(2)
            End If
            If Words.Length > 3 Then
                c4 = Words(3)
            End If
            If Words.Length > 4 Then
                c5 = Words(4)
            End If
            If Words.Length > 5 Then
                c6 = Words(5)
            End If
            If Words.Length > 6 Then
                c7 = Words(6)
            End If
            If Words.Length > 7 Then
                c8 = Words(7)
            End If
            If Words.Length > 8 Then
                c9 = Words(8)
            End If
            If Words.Length > 9 Then
                c10 = Words(9)
            End If
            If Words.Length > 10 Then
                c11 = Words(10)
            End If
            If Words.Length > 11 Then
                c12 = Words(11)
            End If
            If Words.Length > 12 Then
                c13 = Words(12)
            End If
            If Words.Length > 13 Then
                c14 = Words(13)
            End If
            If Words.Length > 14 Then
                c15 = Words(14)
            End If
            If Words.Length > 15 Then
                c16 = Words(15)
            End If
            If Words.Length > 16 Then
                c17 = Words(16)
            End If
            If Words.Length > 17 Then
                c18 = Words(17)
            End If
            If Words.Length > 18 Then
                c19 = Words(18)
            End If
            If Words.Length > 19 Then
                c20 = Words(19)
            End If
            CMDTime()
        End If
我如何才能做到这一点

If Words.Length > ? Then
    Dim c?+1 = Words(?)
End If
如果用户输入单词长度:4 自动生成:

    Dim c5 = Words(4)
    Dim c4 = Words(3)
    Dim c3 = Words(2)
    Dim c2 = Words(1)
    Dim c1 = Words(0)

我的程序的例子;
用户>textbox.text>输入多少单词>尺寸c1>单词1,尺寸c2…

为什么不使用数组?我不完全理解你想做什么,因为我看不到完整的代码

    Dim s As String = "The quick brown fox jumps over the lazy dog"
    Dim Words() As String = s.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
    Dim c(Words.Length - 1) As String
    For x = 0 To Words.Length - 1
        c(x) = Words(x)
    Next

    Console.WriteLine(String.Join("-", c))
    Console.WriteLine(c(0))
    Console.WriteLine(c(1))
输出:

The-quick-brown-fox-jumps-over-the-lazy-dog
The
quick

简单的回答是:不,你不能这样做:

    'Nooooo!
    If Words.Length > ? Then
        Dim c?+1 = Words(?)
    End If
但是。。。你是个程序员,所以一切皆有可能!只是不总是像我们最初想象的那样

在具有动态名称的循环中,不能对变量进行调暗,但也有其他选择。首先,您可以有一个变量列表,可以在循环的每个过程中添加这些变量

但这还不够,因为您的意思是变量名称要有意义。因此,您必须创建一个KeyValuePair列表。键是有意义的数字,值是字符串。像这样:

    Dim myList As New List(Of KeyValuePair(Of Integer, String))

    Dim index As Integer = 0
    For Each s As String In words
        index += 1
        myList.Add(New KeyValuePair(Of Integer, String)(index, s))
    Next
但是,真的,这种方法有点不可靠,使用起来会有不必要的困难

另一方面,您可以在类中将数组字声明为模块变量,并在每次需要时更新它。您以前的代码片段将缩短为:

    Dim s As String = TypeHere.Text ' Heres target: TypeHere.Text
    Words = s.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)

    If Words.Length > 0 Then
        CMDTime()
    End If
现在,你知道单词s0和c1是一样的。你可以改用Words0。不过要小心,因为如果你硬编码这样的索引,如果你越界,有时可能会崩溃。您可以通过在If Words.Length>x-Then块中保护弱代码来避免这种情况,就像您以前所做的那样。或者,我更喜欢的是,如果您必须遍历数组中的每个条目,请在For-Each循环中执行。如果数组中没有项,则此循环不会崩溃

当你想起来的时候,你已经在做这件事了,或者差不多。你一直都有答案!这不是魔法吗

如果这没有帮助,请尝试精确地说明您在寻找什么。一定会有人捡起球来帮忙的


玩得开心

您只需要前两行代码:

Dim s As String = "Hello World"
Dim Words() As String = s.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
然后你可以这样做:

Console.WriteLine(Words.Length)
Console.WriteLine(Words(0))
Console.WriteLine(Words(1))
这将产生:

2 Hello World
只是在你使用c1的地方,用单词0替换它,用单词1替换c2,等等。你会用单词。长度来知道你有多少个单词。

你好!你为什么不使用数组字来代替你的c变量呢?我知道你为什么不想再使用这些代码了,但是我不确定你为什么要创建变量来保存数组中的每个字符串?你在用C1,C2,C3做什么。。。。使用wordx填充变量后,是否可以使用这些变量?为什么不能在C变量所在的地方使用word数组呢?希望我能帮助你更好地理解你想做什么,马克,c怎么样?现在定义的变量?它们还用在哪里?@HasanMerkit如果对你有帮助,请选择这个答案。