我需要使这个代码更有效,例如小错误

时间:2014-01-04 19:02:51

标签: arrays vb.net loops csv

我尝试过学习StreamWriter,但效果不佳(缺乏资源)。我也很感激任何书籍或网站建议。

Dim decision As Char
Dim fullline As String = " "

Dim word(9), def(9), minorerror1(49), minorerror2(49), minorerror3(49), minorerror4(49), minorerror5(49) As String
Dim datetime(1) As System.DateTime

Console.WriteLine("3 - Year 3")
Console.WriteLine("4 - Year 4")
Console.WriteLine("5 - Year 5")
Console.WriteLine("6 - Year 6")

Console.WriteLine()

Dim testyear As Integer = Console.ReadLine()

Select Case testyear
    Case 1
    Case 2
    Case 3
    Case 4
End Select

Console.WriteLine("Please enter 10 of your selected words, defenitions and selected amount minor errors to be featured in the following test")

FileOpen(5, "F:\Computing\Spelling Bee\testtests.csv", OpenMode.Append)

Do
    counter = counter + 1
    Console.Write("Word: ")
    word(counter) = Console.ReadLine
    Console.Write("Defenition: ")
    def(counter) = Console.ReadLine

    Console.WriteLine("Type in a max of 5 minor errors")

    Console.Write("Minor error 1: ")
    minorerror1(counter) = Console.ReadLine
    Console.Write("Minor error 2: ")
    minorerror2(counter) = Console.ReadLine
    Console.Write("Minor error 3: ")
    minorerror3(counter) = Console.ReadLine
    Console.Write("Minor error 4: ")
    minorerror4(counter) = Console.ReadLine
    Console.Write("Minor error 5: ")
    minorerror5(counter) = Console.ReadLine
Loop Until counter = 9
Console.Clear()

For counter As Integer = 0 To 9
    fullline = word(counter) & "," & def(counter) & "," & minorerror1(counter) & "," & minorerror2(counter) & "," & minorerror3(counter) & "," & minorerror4(counter) & "," & minorerror5(counter)
    PrintLine(5, fullline)
    Console.WriteLine("Word: " & (word(counter.ToString)) & " Defenition: " & (def(counter.ToString)) & " Minor error 1: " & (minorerror1(counter.ToString)) & " Minor error 2: " & (minorerror2(counter.ToString))& " Minor error 3: " & (minorerror3(counter.ToString))& " Minor error 4: " & (minorerror4(counter.ToString))& " Minor error 5: " & (minorerror5(counter.ToString))
Next

'上面的内容肯定需要抛光,但我真的不知道如何存储小错误。

Console.WriteLine("Are you sure you want to save this test? (y/n) ")
decision = Console.ReadLine
If decision = "y" Or decision = "Y" Then
    FileClose(5)
    staffmenu()
ElseIf decision = "n" Or decision = "N" Then
    addtest()
End If

3 个答案:

答案 0 :(得分:1)

Dim myLines As New List(of String)
Dim sb as StringBuilder

For counter As Integer = 0 To 9

   sb = New StringBuilder

   Console.Write("Word: ")
   sb.Append(Console.ReadLine & ", ")

   Console.Write("Defenition: ")            ' (sic)
   sb.Append(Console.ReadLine & ", ")

   Console.WriteLine("Type in a max of 5 minor errors")

  ' the five (? - the arrays are set much larger)
  ' minor errors can be an inner loop:
   For j as integer = 1 to 5                 ' zero based confuses users
       Console.Write("Minor error " & j.ToString & ": ")
       sb.Append(Console.ReadLine & ", ")
   Next j

   myLines.Add(sb.ToString)

Next counter

Using sw As New StreamWriter("theFileName.CSV")
   For n As Integer = 0 to myLines.Count - 1
        sw.WriteLine(myLines(n))
   Next n
End Using

那应该非常接近

答案 1 :(得分:1)

我了解StreamWriterStreamReader的问题,因为我自己刚学会了这些问题。如果它有帮助,这可能是你可以得到的最基本但它确实让你去。通过循环和类似结构的基本知识,StreamWriter可以变得非常强大:

Basic StreamWriter

StreamReader

Basic StreamReader

答案 2 :(得分:0)

您想提高效率还是更灵活/可管理?

如果你在循环的每次迭代中从控制台读取五次,那么尝试加速它是没有意义的;甚至让(imho)进入stringbuilder。

更灵活的解决方案是:

Const MaxNumberOfMinorErrors为integer = 5

Dim Minorerror(MaxNumberOfMinorErrors - 1,9)作为字符串

...

对于x为Integer = 1到MaxNumberOfMinorErrors

Console.WriteLine("Minor error {0}: ", x)
minorerror(x- 1, Counter) = Console.ReadLine

下一步