0

我的控制台应用程序末尾有一个程序,要求用户按 Enter 键结束程序,或键入 s 执行另一次搜索。我遇到的问题是无论用户在程序结束时输入什么。我想要发生的是,如果用户键入 s,那么程序会重新启动并让用户执行另一次搜索。该程序是程序性的,我需要找到一种方法从程序顶部开始,要求用户键入要搜索的名称。我怎样才能做到这一点?

这是我遇到问题的程序:

Private Sub terminateProgram()
    Console.Write("Press the enter key to end the program or press s to search again")
    If (CStr(Console.ReadLine()) = "s") Then
        Console.WriteLine("It worked")
    Else
        Console.Read()
    End If

注意:如您所见,我只是在窗口中写入“它工作”只是为了至少尝试捕获正在输入的“s”。

4

1 回答 1

1

在循环中构造你的程序,你的方法返回一个布尔值,这样只要你的方法返回“False”,程序就会继续运行:

Sub Main()
   Do

   Loop While Not terminateProgram()
End Sub

Public Function terminateProgram() As Boolean
    Console.Write("Press the s key to search again or any other key to end the program")
    Return Console.ReadKey(True).KeyChar.ToLower() <> "s" 
End Function
于 2012-04-08T20:57:58.507 回答