是的,我想知道如何(如果可能的话)使用已经阅读的代码或执行此操作的有效方法。
我希望能够使用位置变量而不必将其写出数百次。希望你理解我在说什么。我希望能够离开商店,然后返回此console.writeline(“您想去哪里”)部分。
Console.WriteLine("where would you like to go")
Console.WriteLine("1 - The shop")
location = Console.ReadLine()
Loop Until location = "1"
Console.WriteLine("")
Console.WriteLine("")
Console.WriteLine("")
Console.WriteLine("***********************************The Shop***********************************")
Console.ReadLine()
Console.WriteLine("")
Console.writeline("Shopkeeper: how can I help you")
我建议您尝试一种更有条理的方法。使用一个名为的新类Location
,其中包含有关每个位置的信息(示例包含名称和可能的目的地列表)。当然,可以通过可能的交互作用或类似的方式进一步推进这一过程。
Public Class Location
Public Property Name As String
Public Property Destinations As List(Of String)
Public Sub New(Name As String, Destinations As String())
Me.Name = Name
Me.Destinations = New List(Of String)
Me.Destinations.AddRange(Destinations)
End Sub
End Class
首先,您要列出游戏中的位置。我做了三个,分别是街道,商店和商店的后室(神秘!)。在每次迭代中,您将显示一个列表,其中列出了所创建对象的位置,并让用户选择一个。然后,您可以根据名称更改位置。
这样,您可以轻松添加位置和互连。
您实际上并不想对用户可以采取的每个步骤进行硬编码。
Module Module1
Sub Main()
'Create locations
Dim Locations As New List(Of Location)
Locations.Add(New Location("Shop", {"Street", "Back Room"}))
Locations.Add(New Location("Street", {"Shop"}))
Locations.Add(New Location("Back Room", {"Shop"}))
'Define a starting location
Dim CurrentLocation As String = "Street"
Do
Console.WriteLine("You are at: " & CurrentLocation)
Console.WriteLine("Destinations: ")
'Bit of Linq to select the location by name from your defined locations
Dim ThisLocation As Location = (From l As Location In Locations Where l.Name = CurrentLocation Select l).First
'Display the possible destinations from here
For i = 0 To ThisLocation.Destinations.Count - 1
Console.WriteLine(String.Format("-{0}- {1}", (i + 1).ToString, ThisLocation.Destinations(i)))
Next
'Read user input for a the destination he wants to travel to
Dim NewLocation As Integer = -1
Do
Console.Write(" Go to: ")
Integer.TryParse(Console.ReadLine, NewLocation)
Loop Until NewLocation >= 1 AndAlso NewLocation <= ThisLocation.Destinations.Count
'Change the current location
CurrentLocation = ThisLocation.Destinations(NewLocation - 1)
Loop
End Sub
End Module
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句