I am slice a string from a text file and put it into an array, however whenever I output the array they are filled with 0's

huangapple go评论66阅读模式
英文:

I am slice a string from a text file and put it into an array, however whenever I output the array they are filled with 0's

问题

以下是您的代码的翻译部分:

我的代码基本上将3个随机变量输入到文本文件中使用:分隔每个变量我的代码的第二部分基本上再次读取文本文件并使用我之前输入的 MID 函数来切割字符串以获取第二个变量然后将其添加到一个数组中然而当我输出数组时它都是0我尝试找出问题所在但我不知道

以下是我的代码

Dim r1, r2, r3, counter, counter1, counter2, sep As Integer
Dim line As String
Dim middleline(20)
line = ""
counter1 = 0
sep = 0
FileSystem.FileOpen(1, "C:\Users\user\OneDrive\Desktop\binarysearch.txt", OpenMode.Output, OpenAccess.Write)
For counter = 1 To 20
    If Rnd() = 0.21 Then
        r1 = -1
    Else
        Do
            r1 = Rnd() * 100
        Loop Until r1 <= 20
    End If
    Do
        r2 = Rnd() * 100
    Loop Until r2 >= 10 AndAlso r2 <= 50
    If Rnd() = 0.21 Then
        r3 = -1
    Else
        Do
            r3 = Rnd() * 100
        Loop Until r3 <= 20
    End If
    line = r1 & ":" & r2 & ":" & r3
    FileSystem.WriteLine(1, line)
Next
Console.WriteLine("生成成功。")

For counter = 1 To 20
    middleline(counter) = 0
Next

For counter = 1 To 20
    FileSystem.PrintLine(1, line)
    For counter1 = 1 To Len(line)
        If Mid(line, 1, counter1) = ":" Then
            sep += 1
        End If
        If sep = 1 Then
            middleline(counter2) = middleline(counter2) & Mid(line, 1, counter1)
            counter2 += counter2
        End If
    Next
Next

For counter = 1 To 20
    Console.WriteLine(middleline(counter))
Next
Console.WriteLine(middleline)

希望这能帮助您理解代码。如果您需要进一步的解释或帮助,请告诉我。

英文:

My code basically inputs 3 random variables into a text file, with a ":" separating each variable. And the second part of my code basically reads the text file again, and slices the string to get the 2nd variable using the MID function that I input previously and adds it into an array. However when I output the array, it's full of 0s. I tried figuring out what's wrong but I don't know.

Here is my code:

Dim r1, r2, r3, counter, counter1, counter2, sep As Integer
Dim line As String
Dim middleline(20)
line = &quot;&quot;
counter1 = 0
sep = 0
FileSystem.FileOpen(1, &quot;C:\Users\user\OneDrive\Desktop\binarysearch.txt&quot;, OpenMode.Output, OpenAccess.Write)
For counter = 1 To 20
If Rnd() = 0.21 Then
r1 = -1
Else
Do
r1 = Rnd() * 100
Loop Until r1 &lt;= 20
End If
Do
r2 = Rnd() * 100
Loop Until r2 &gt;= 10 AndAlso r2 &lt;= 50
If Rnd() = 0.21 Then
r3 = -1
Else
Do
r3 = Rnd() * 100
Loop Until r3 &lt;= 20
End If
line = r1 &amp; &quot;:&quot; &amp; r2 &amp; &quot;:&quot; &amp; r3
FileSystem.WriteLine(1, line)
Next
Console.WriteLine(&quot;Generated successfully.&quot;)
For counter = 1 To 20
middleline(counter) = 0
Next
For counter = 1 To 20
FileSystem.PrintLine(1, line)
For counter1 = 1 To Len(line)
If Mid(line, 1, counter1) = &quot;:&quot; Then
sep += 1
End If
If sep = 1 Then
middleline(counter2) = middleline(counter2) &amp; Mid(line, 1, counter1)
counter2 += counter2
End If
Next
Next
For counter = 1 To 20
Console.WriteLine(middleline(counter))
Next
Console.WriteLine(middleline)

答案1

得分: 3

以下是您要翻译的代码部分:

I'm not 100% sure where your code goes wrong, but I have given it a little rewrite and this appears to be working ok.
&lt;br&gt;I changed the random number generation as I felt your loop was a little heavy duty.
&lt;br&gt;Also switched the file handling to newer methods.

Private Shared RndGenMachine As Random = New Random

Public Sub DoWork(FileName As String)

    Dim r1, r2, r3 As Integer
    Dim middleline As List(Of String) = New List(Of String)

    &#39;build the initial file
    Using sw As New StreamWriter(FileName, True)

        For counter As Integer = 1 To 20

            r1 = RndGenMachine.Next(0, 20)
            r3 = RndGenMachine.Next(0, 20)
            r2 = RndGenMachine.Next(10, 50)

            Dim line As String = $&quot;{r1}:{r2}:{r3}&quot;
            sw.WriteLine(line)

        Next

    End Using

    Console.WriteLine(&quot;Generated successfully.&quot;)

    &#39;read it back for parsing
    Dim lines As IEnumerable(Of String) = File.ReadLines(FileName)

    For lineCounter = 0 To lines.Count - 1

        Dim splits() As String = lines(lineCounter).Split(Convert.ToChar(&quot;:&quot;))
        middleline.Add(splits(1))

    Next

    For Each middlelinevalue As String In middleline
        Console.WriteLine(middlelinevalue)
    Next

End Sub

如果您想使用旧式文件处理方式,可以尝试以下代码:

Public Sub DoWorkOldStyle(FileName As String, iterations As Integer)

    Console.WriteLine(&quot;Starting generation...&quot;)

    Dim r1, r2, r3 As Integer

    &#39;build the initial file
    FileSystem.FileOpen(1, FileName, OpenMode.Output, OpenAccess.Write)

    For counter As Integer = 1 To iterations

        r1 = RndGenMachine.Next(0, 20)
        r3 = RndGenMachine.Next(0, 20)
        r2 = RndGenMachine.Next(10, 50)

        Dim line As String = $&quot;{r1}:{r2}:{r3}&quot;
        FileSystem.WriteLine(1, line)

    Next

    FileSystem.FileClose(1)

    Console.WriteLine(&quot;Generated successfully. Starting evaluation...&quot;)

    &#39;read it back for parsing
    FileSystem.FileOpen(1, FileName, OpenMode.Input, OpenAccess.Read)

    Dim middleline As List(Of String) = New List(Of String)

    While Not EOF(1)

        Dim line As String = LineInput(1)
        Dim splits() As String = line.Split(Convert.ToChar(&quot;:&quot;))
        middleline.Add(splits(1))

    End While

    FileClose(1)

    For Each middlelinevalue As String In middleline
        Console.WriteLine(middlelinevalue)
    Next

    Console.WriteLine(&quot;Evalutaion complete.&quot;)

End Sub

请注意,我已经将HTML转义字符(<和>)还原为它们的原始形式。

英文:

I'm not 100% sure where your code goes wrong, but I have given it a little rewrite and this appears to be working ok.
<br>I changed the random number generation as I felt your loop was a little heavy duty.
<br>Also switched the file handling to newer methods.

    Private Shared RndGenMachine As Random = New Random
Public Sub DoWork(FileName As String)
Dim r1, r2, r3 As Integer
Dim middleline As List(Of String) = New List(Of String)
&#39;build the initial file
Using sw As New StreamWriter(FileName, True)
For counter As Integer = 1 To 20
r1 = RndGenMachine.Next(0, 20)
r3 = RndGenMachine.Next(0, 20)
r2 = RndGenMachine.Next(10, 50)
Dim line As String = $&quot;{r1}:{r2}:{r3}&quot;
sw.WriteLine(line)
Next
End Using
Console.WriteLine(&quot;Generated successfully.&quot;)
&#39;read it back for parsing
Dim lines As IEnumerable(Of String) = File.ReadLines(FileName)
For lineCounter = 0 To lines.Count - 1
Dim splits() As String = lines(lineCounter).Split(Convert.ToChar(&quot;:&quot;))
middleline.Add(splits(1))
Next
For Each middlelinevalue As String In middleline
Console.WriteLine(middlelinevalue)
Next
End Sub

If you'd prefer to use the old style file handler, you can try something like this:

    Public Sub DoWorkOldStyle(FileName As String, iterations As Integer)
Console.WriteLine(&quot;Starting generation...&quot;)
Dim r1, r2, r3 As Integer
&#39;build the initial file
FileSystem.FileOpen(1, FileName, OpenMode.Output, OpenAccess.Write)
For counter As Integer = 1 To iterations
r1 = RndGenMachine.Next(0, 20)
r3 = RndGenMachine.Next(0, 20)
r2 = RndGenMachine.Next(10, 50)
Dim line As String = $&quot;{r1}:{r2}:{r3}&quot;
FileSystem.WriteLine(1, line)
Next
FileSystem.FileClose(1)
Console.WriteLine(&quot;Generated successfully. Starting evaluation...&quot;)
&#39;read it back for parsing
FileSystem.FileOpen(1, FileName, OpenMode.Input, OpenAccess.Read)
Dim middleline As List(Of String) = New List(Of String)
While Not EOF(1)
Dim line As String = LineInput(1)
Dim splits() As String = line.Split(Convert.ToChar(&quot;:&quot;))
middleline.Add(splits(1))
End While
FileClose(1)
For Each middlelinevalue As String In middleline
Console.WriteLine(middlelinevalue)
Next
Console.WriteLine(&quot;Evalutaion complete.&quot;)
End Sub

huangapple
  • 本文由 发表于 2023年3月21日 00:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定