vb.net - 在这种情况下我将如何替换 GoTo?
vb.net - How would I replace GoTo in this instance?
GoTo 曾经在案件执行后 return 进入菜单,因为使用 GoTo 是一种不好的做法,我还能做什么?谢谢
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
下面的伪代码。
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
您需要一个布尔变量来控制包含您的代码的 while 循环的退出。
当布尔变量为真时,您继续循环读取用户输入、处理它并重新打印菜单选项。将布尔变量设置为 false 将终止循环。
不要忘记提供终止程序的选项
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
GoTo 曾经在案件执行后 return 进入菜单,因为使用 GoTo 是一种不好的做法,我还能做什么?谢谢
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
下面的伪代码。
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
您需要一个布尔变量来控制包含您的代码的 while 循环的退出。
当布尔变量为真时,您继续循环读取用户输入、处理它并重新打印菜单选项。将布尔变量设置为 false 将终止循环。
不要忘记提供终止程序的选项
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