英文:
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 = ""
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("Generated successfully.")
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)
答案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.
<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)
'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 = $"{r1}:{r2}:{r3}"
sw.WriteLine(line)
Next
End Using
Console.WriteLine("Generated successfully.")
'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(":"))
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("Starting generation...")
Dim r1, r2, r3 As Integer
'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 = $"{r1}:{r2}:{r3}"
FileSystem.WriteLine(1, line)
Next
FileSystem.FileClose(1)
Console.WriteLine("Generated successfully. Starting evaluation...")
'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(":"))
middleline.Add(splits(1))
End While
FileClose(1)
For Each middlelinevalue As String In middleline
Console.WriteLine(middlelinevalue)
Next
Console.WriteLine("Evalutaion complete.")
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)
'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 = $"{r1}:{r2}:{r3}"
sw.WriteLine(line)
Next
End Using
Console.WriteLine("Generated successfully.")
'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(":"))
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("Starting generation...")
Dim r1, r2, r3 As Integer
'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 = $"{r1}:{r2}:{r3}"
FileSystem.WriteLine(1, line)
Next
FileSystem.FileClose(1)
Console.WriteLine("Generated successfully. Starting evaluation...")
'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(":"))
middleline.Add(splits(1))
End While
FileClose(1)
For Each middlelinevalue As String In middleline
Console.WriteLine(middlelinevalue)
Next
Console.WriteLine("Evalutaion complete.")
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论