[英]vb.net - How would I replace GoTo in this instance?
GoTo used to return to menu after a case was carried out, since its bad practice to use GoTo what else could I do? GoTo通常在执行完一个案例后返回菜单,因为使用GoTo的错误做法是我还能做什么? Thanks.
谢谢。
Menu:
Console.WriteLine("----------------------")
Console.WriteLine("1 = option1")
Console.WriteLine("2 = option2")
Console.WriteLine("----------------------")
Console.Write("Select an option: ")
opts = Console.ReadLine
Console.WriteLine("----------------------")
Select Case opts
Case 1
Happening:
Try
Console.Write("Enter a word: ")
word = Console.ReadLine
Select Case word.ToLower
Case Eng(0)
Console.WriteLine(Fre(0))
Case Eng(1)
Console.WriteLine(Fre(1))
GoTo Menu
End Select
Catch ex As Exception
Console.WriteLine("Invalid input")
End Try
You need a boolean variable to control the exit from a while loop that encloses your code. 您需要一个布尔变量来控制从封装您的代码的while循环的退出。
While the boolean variable is true you continue your loop reading the user input, processing it and reprinting the menu choices. 当布尔变量为true时,您将继续循环读取用户输入,对其进行处理并重新打印菜单选项。 Setting the boolean variable to false will terminate the loop.
将布尔变量设置为false将终止循环。
Don't forget to offer an option to terminate the program 不要忘记提供终止程序的选项
Dim runLoop = true
while runLoop
Console.WriteLine("----------------------")
Console.WriteLine("1 = option1")
Console.WriteLine("2 = option2")
Console.WriteLine("3 = EXIT")
Console.WriteLine("----------------------")
Console.Write("Select an option: ")
opts = Console.ReadLine
Console.WriteLine("----------------------")
Select Case opts
Case "1"
Try
Console.Write("Enter a word: ")
word = Console.ReadLine
Select Case word.ToLower
Case Eng(0)
Console.WriteLine(Fre(0))
Case Eng(1)
Console.WriteLine(Fre(1))
runLoop = false
End Select
Catch ex As Exception
Console.WriteLine("Invalid input")
End Try
case "2"
.....
case "3"
runLoop = false
End Select
End While
Pseudo Code Below. 下面的伪代码。
Dim goodResultExists As Boolean = False
While Not goodResultExists
Console.Write("Enter a word: ")
word = Console.ReadLine
Select Case word.ToLower
case "Ok"
goodResultExists = True
Case Else
Console.Write("Invalid Entry")
End Select
End While
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.