Vb控制台问题如何

时间:2014-09-06 13:56:01

标签: vb.net vba console

对,我想知道如何使用已经阅读的代码或有效的方法。

我希望能够使用位置变量而不必将其写出数百次。 希望你明白我在说什么。我希望能够离开商店并回到这个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")

1 个答案:

答案 0 :(得分:1)

我建议你尝试更有条理的方法。使用名为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
相关问题