0

我在学校做 VB,我们正在搜索一个文本文件。但是,如果没有找到结果,我想输出一次“没有结果”。我已经尝试过了,但它会为文本文件中的每一行代码输出它。我该怎么做?

Module Module1
Dim townname, findtown, findname, teamname, coachname, phone As String
Dim choice As String
Sub Main()

    Do
        Console.WriteLine("Please pick an option:")
        Console.WriteLine("1. Search team by team name - Press 1")
        Console.WriteLine("2. Search team by town name - Press 2")
        Console.WriteLine("3. End Program - Press 3")

        choice = Console.ReadLine

        Select Case choice


            Case 1
                Console.Write("Enter the team name:")
                findname = Console.ReadLine()
                FileOpen(1, "TeamdataFile.txt", OpenMode.Input)

                Do
                    Input(1, teamname)
                    Input(1, townname)
                    Input(1, coachname)
                    Input(1, phone)

                    If teamname.ToLower = findname.ToLower Then

                        Console.WriteLine("Team: {0}, from {1}, coach: {2}, contact: {3}", teamname, townname, coachname, phone)
                    End If

                Loop Until EOF(1)
                FileClose(1)
                Console.ReadLine()

            Case 2
                Console.Write("Enter the town the team is from:")
                findtown = Console.ReadLine()
                FileOpen(1, "TeamdataFile.txt", OpenMode.Input)

                Do
                    Input(1, teamname)
                    Input(1, townname)
                    Input(1, coachname)
                    Input(1, phone)

                    If townname.ToLower = findtown.ToLower Then
                        Console.WriteLine("Team: {0}, from {1}, coach: {2}, contact: {3}", teamname, townname, coachname, phone)
                    End If

                Loop Until EOF(1)
                FileClose(1)
                Console.ReadLine()
            Case 3
                End
            Case Else
                Console.WriteLine("Invalid option - Choose option 1 or 2 or 3")
        End Select
    Loop Until choice = "1" Or choice = "2" Or choice = "3"
End Sub

端模块

4

1 回答 1

1

无需立即对找到的名称做出反应,只需将布尔变量设置为 true,让检查一直持续到文件末尾或立即中断循环。循环退出后根据布尔值打印结果

   Dim foundResult As Boolean

   .....
   Select Case choice
        Case 1
            Console.Write("Enter the team name:")
            findname = Console.ReadLine()
            FileOpen(1, "TeamdataFile.txt", OpenMode.Input)
            foundResult = False
            Do
                Input(1, teamname)
                Input(1, townname)
                Input(1, coachname)
                Input(1, phone)

                If teamname.ToLower = findname.ToLower Then
                    foundResult = True
                    Exit While
                End If

            Loop Until EOF(1)
            FileClose(1)
            if foundResult = True Then
                Console.WriteLine("Team: {0}, from {1}, coach: {2}, contact: {3}", teamname, townname, coachname, phone)
            else
                Console.WriteLine("There are no results"
            End if
      ......

附带说明一下,您真的应该停止使用老式的 FileOpen、Input、FileClose。这些函数在这里只是为了与 VB6 兼容。NET Framework 具有更好的文件处理方法。您应该使用专为 NET 设计的StreamReader及其方法。所有的 System.IO 命名空间都要强大一个数量级。

于 2013-06-08T14:22:50.380 回答