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评论95阅读模式
英文:

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

问题

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

  1. 我的代码基本上将3个随机变量输入到文本文件中使用:分隔每个变量我的代码的第二部分基本上再次读取文本文件并使用我之前输入的 MID 函数来切割字符串以获取第二个变量然后将其添加到一个数组中然而当我输出数组时它都是0我尝试找出问题所在但我不知道
  2. 以下是我的代码
  3. Dim r1, r2, r3, counter, counter1, counter2, sep As Integer
  4. Dim line As String
  5. Dim middleline(20)
  6. line = ""
  7. counter1 = 0
  8. sep = 0
  9. FileSystem.FileOpen(1, "C:\Users\user\OneDrive\Desktop\binarysearch.txt", OpenMode.Output, OpenAccess.Write)
  10. For counter = 1 To 20
  11. If Rnd() = 0.21 Then
  12. r1 = -1
  13. Else
  14. Do
  15. r1 = Rnd() * 100
  16. Loop Until r1 <= 20
  17. End If
  18. Do
  19. r2 = Rnd() * 100
  20. Loop Until r2 >= 10 AndAlso r2 <= 50
  21. If Rnd() = 0.21 Then
  22. r3 = -1
  23. Else
  24. Do
  25. r3 = Rnd() * 100
  26. Loop Until r3 <= 20
  27. End If
  28. line = r1 & ":" & r2 & ":" & r3
  29. FileSystem.WriteLine(1, line)
  30. Next
  31. Console.WriteLine("生成成功。")
  32. For counter = 1 To 20
  33. middleline(counter) = 0
  34. Next
  35. For counter = 1 To 20
  36. FileSystem.PrintLine(1, line)
  37. For counter1 = 1 To Len(line)
  38. If Mid(line, 1, counter1) = ":" Then
  39. sep += 1
  40. End If
  41. If sep = 1 Then
  42. middleline(counter2) = middleline(counter2) & Mid(line, 1, counter1)
  43. counter2 += counter2
  44. End If
  45. Next
  46. Next
  47. For counter = 1 To 20
  48. Console.WriteLine(middleline(counter))
  49. Next
  50. 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:

  1. Dim r1, r2, r3, counter, counter1, counter2, sep As Integer
  2. Dim line As String
  3. Dim middleline(20)
  4. line = &quot;&quot;
  5. counter1 = 0
  6. sep = 0
  7. FileSystem.FileOpen(1, &quot;C:\Users\user\OneDrive\Desktop\binarysearch.txt&quot;, OpenMode.Output, OpenAccess.Write)
  8. For counter = 1 To 20
  9. If Rnd() = 0.21 Then
  10. r1 = -1
  11. Else
  12. Do
  13. r1 = Rnd() * 100
  14. Loop Until r1 &lt;= 20
  15. End If
  16. Do
  17. r2 = Rnd() * 100
  18. Loop Until r2 &gt;= 10 AndAlso r2 &lt;= 50
  19. If Rnd() = 0.21 Then
  20. r3 = -1
  21. Else
  22. Do
  23. r3 = Rnd() * 100
  24. Loop Until r3 &lt;= 20
  25. End If
  26. line = r1 &amp; &quot;:&quot; &amp; r2 &amp; &quot;:&quot; &amp; r3
  27. FileSystem.WriteLine(1, line)
  28. Next
  29. Console.WriteLine(&quot;Generated successfully.&quot;)
  30. For counter = 1 To 20
  31. middleline(counter) = 0
  32. Next
  33. For counter = 1 To 20
  34. FileSystem.PrintLine(1, line)
  35. For counter1 = 1 To Len(line)
  36. If Mid(line, 1, counter1) = &quot;:&quot; Then
  37. sep += 1
  38. End If
  39. If sep = 1 Then
  40. middleline(counter2) = middleline(counter2) &amp; Mid(line, 1, counter1)
  41. counter2 += counter2
  42. End If
  43. Next
  44. Next
  45. For counter = 1 To 20
  46. Console.WriteLine(middleline(counter))
  47. Next
  48. Console.WriteLine(middleline)

答案1

得分: 3

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

  1. 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.
  2. &lt;br&gt;I changed the random number generation as I felt your loop was a little heavy duty.
  3. &lt;br&gt;Also switched the file handling to newer methods.
  4. Private Shared RndGenMachine As Random = New Random
  5. Public Sub DoWork(FileName As String)
  6. Dim r1, r2, r3 As Integer
  7. Dim middleline As List(Of String) = New List(Of String)
  8. &#39;build the initial file
  9. Using sw As New StreamWriter(FileName, True)
  10. For counter As Integer = 1 To 20
  11. r1 = RndGenMachine.Next(0, 20)
  12. r3 = RndGenMachine.Next(0, 20)
  13. r2 = RndGenMachine.Next(10, 50)
  14. Dim line As String = $&quot;{r1}:{r2}:{r3}&quot;
  15. sw.WriteLine(line)
  16. Next
  17. End Using
  18. Console.WriteLine(&quot;Generated successfully.&quot;)
  19. &#39;read it back for parsing
  20. Dim lines As IEnumerable(Of String) = File.ReadLines(FileName)
  21. For lineCounter = 0 To lines.Count - 1
  22. Dim splits() As String = lines(lineCounter).Split(Convert.ToChar(&quot;:&quot;))
  23. middleline.Add(splits(1))
  24. Next
  25. For Each middlelinevalue As String In middleline
  26. Console.WriteLine(middlelinevalue)
  27. Next
  28. End Sub

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

  1. Public Sub DoWorkOldStyle(FileName As String, iterations As Integer)
  2. Console.WriteLine(&quot;Starting generation...&quot;)
  3. Dim r1, r2, r3 As Integer
  4. &#39;build the initial file
  5. FileSystem.FileOpen(1, FileName, OpenMode.Output, OpenAccess.Write)
  6. For counter As Integer = 1 To iterations
  7. r1 = RndGenMachine.Next(0, 20)
  8. r3 = RndGenMachine.Next(0, 20)
  9. r2 = RndGenMachine.Next(10, 50)
  10. Dim line As String = $&quot;{r1}:{r2}:{r3}&quot;
  11. FileSystem.WriteLine(1, line)
  12. Next
  13. FileSystem.FileClose(1)
  14. Console.WriteLine(&quot;Generated successfully. Starting evaluation...&quot;)
  15. &#39;read it back for parsing
  16. FileSystem.FileOpen(1, FileName, OpenMode.Input, OpenAccess.Read)
  17. Dim middleline As List(Of String) = New List(Of String)
  18. While Not EOF(1)
  19. Dim line As String = LineInput(1)
  20. Dim splits() As String = line.Split(Convert.ToChar(&quot;:&quot;))
  21. middleline.Add(splits(1))
  22. End While
  23. FileClose(1)
  24. For Each middlelinevalue As String In middleline
  25. Console.WriteLine(middlelinevalue)
  26. Next
  27. Console.WriteLine(&quot;Evalutaion complete.&quot;)
  28. 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.

  1. Private Shared RndGenMachine As Random = New Random
  2. Public Sub DoWork(FileName As String)
  3. Dim r1, r2, r3 As Integer
  4. Dim middleline As List(Of String) = New List(Of String)
  5. &#39;build the initial file
  6. Using sw As New StreamWriter(FileName, True)
  7. For counter As Integer = 1 To 20
  8. r1 = RndGenMachine.Next(0, 20)
  9. r3 = RndGenMachine.Next(0, 20)
  10. r2 = RndGenMachine.Next(10, 50)
  11. Dim line As String = $&quot;{r1}:{r2}:{r3}&quot;
  12. sw.WriteLine(line)
  13. Next
  14. End Using
  15. Console.WriteLine(&quot;Generated successfully.&quot;)
  16. &#39;read it back for parsing
  17. Dim lines As IEnumerable(Of String) = File.ReadLines(FileName)
  18. For lineCounter = 0 To lines.Count - 1
  19. Dim splits() As String = lines(lineCounter).Split(Convert.ToChar(&quot;:&quot;))
  20. middleline.Add(splits(1))
  21. Next
  22. For Each middlelinevalue As String In middleline
  23. Console.WriteLine(middlelinevalue)
  24. Next
  25. End Sub

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

  1. Public Sub DoWorkOldStyle(FileName As String, iterations As Integer)
  2. Console.WriteLine(&quot;Starting generation...&quot;)
  3. Dim r1, r2, r3 As Integer
  4. &#39;build the initial file
  5. FileSystem.FileOpen(1, FileName, OpenMode.Output, OpenAccess.Write)
  6. For counter As Integer = 1 To iterations
  7. r1 = RndGenMachine.Next(0, 20)
  8. r3 = RndGenMachine.Next(0, 20)
  9. r2 = RndGenMachine.Next(10, 50)
  10. Dim line As String = $&quot;{r1}:{r2}:{r3}&quot;
  11. FileSystem.WriteLine(1, line)
  12. Next
  13. FileSystem.FileClose(1)
  14. Console.WriteLine(&quot;Generated successfully. Starting evaluation...&quot;)
  15. &#39;read it back for parsing
  16. FileSystem.FileOpen(1, FileName, OpenMode.Input, OpenAccess.Read)
  17. Dim middleline As List(Of String) = New List(Of String)
  18. While Not EOF(1)
  19. Dim line As String = LineInput(1)
  20. Dim splits() As String = line.Split(Convert.ToChar(&quot;:&quot;))
  21. middleline.Add(splits(1))
  22. End While
  23. FileClose(1)
  24. For Each middlelinevalue As String In middleline
  25. Console.WriteLine(middlelinevalue)
  26. Next
  27. Console.WriteLine(&quot;Evalutaion complete.&quot;)
  28. 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:

确定