Vb.net方法没有返回值

时间:2016-06-04 20:13:28

标签: vb.net

如果我的isPickedFalse,我的代码返回-1时遇到问题 GetWoodTypes方法,我的if语句被完全忽略,我无法在代码中找到问题,如果有人可以提供帮助那就很好

Public Class Class1

    Shared Sub Main()
        Dim woodType() As String = {"Pine", "Oak", "Elm"}
        Dim woodPrice() As Integer = {100, 140, 200}
        Dim nameIndex As Integer = 0
        nameIndex = GetWoodTypes(woodType, woodPrice)
        Dim Quantity As Integer = 0
        Dim basePrice As Integer = 0
        Quantity = GetDrawerQtys()
        basePrice = GetPrices(woodType, woodPrice, nameIndex, Quantity)


        If nameIndex = -1 Then
            Console.Write("You have made the wrong selection, Please try Again")
            Console.ReadLine()
        Else
            DisplayResults(Quantity, woodType, woodPrice, basePrice, nameIndex)
        End If
    End Sub

    Public Shared Function GetWoodTypes(values() As String, Price() As Integer)
        Console.Write("Please select the type of wood you would like to purchase: Pine,Oak,Elm ")
        Dim Selectedwood As String = Console.ReadLine()
        Dim ispicked As Boolean = False
        Dim location As Integer = 0
        For i As Integer = 0 To values.Count - 1
            If Selectedwood.ToLower = values(i).ToLower Then
                ispicked = True
                location = i
            End If
        Next i
        If ispicked  Then
            Console.WriteLine("Thank you for selection, you have picked {0} at {1} ", values(location), Price(location))
            Return location
        Else
            Console.Write("Sorry")
            Return -1
        End If
    End Function

    Public Shared Function GetDrawerQtys()
        Console.WriteLine(" How Many Drawers do you want ")
        Dim DrawerQty As Integer = Console.ReadLine()
        Return DrawerQty
    End Function

    Public Shared Function GetPrices(types() As String, price() As Integer, location As Integer, Quantity As Integer)
        Console.Write("You have ordered a {0} with {1} drawers at {2} each ", types(location), Quantity, price(location))
        Dim Amount As Integer = (Quantity * 30) + price(location)
        Return Amount
    End Function

    Public Shared Sub DisplayResults(Quantity As Integer, type() As String, price() As Integer, basePrice As Integer, location As Integer)

        Console.WriteLine("Hello you have purchased {0} {1} at {2} your total is {3} ", Quantity, type(location), price(location), basePrice)
        Console.ReadLine()
    End Sub
End Class

2 个答案:

答案 0 :(得分:1)

我会以不同的方式处理这个问题。

首先,我会定义一个单独的类来保存特定木材的类型和价格:

Public Class Wood
    Public Property Type As String
    Public Property Price As Integer
End Class

然后我会创建一个单独的类来进行木材和数量选择,计算价格并显示订单结果。

注意:为了清楚起见,还应声明函数的返回类型,例如
Public Function GetSomeNumber() As Integer

我还使用了.Select().FirstOrDefault()方法。它们是LINQ的一部分。他们接受lambda expression作为参数。万一你还没有见过他们。

Public Class Shop

    Dim woods As List(Of Wood)

    Public Sub New()

        ' Initialize the "product catalogue" using collection initializer combined with object initializer.
        woods = New List(Of Wood) From
        {
            New Wood() With {.Type = "Pine", .Price = 100},
            New Wood() With {.Type = "Oak", .Price = 140},
            New Wood() With {.Type = "Elm", .Price = 200}
        }

    End Sub

    Public Function SelectWood() As Wood

        Dim selectedWood As Wood = Nothing
        Dim woodTypes As String = String.Join(" / ", woods.Select(Function(wood) wood.Type).ToArray())

        While True

            Console.Write("Please select the type of wood you would like to purchase [{0}]: ", woodTypes)

            Dim selectedWoodType As String = Console.ReadLine()

            ' Instead of ToLower() you may use String.Equals() so that you can specify if you want to compare ignoring case.
            ' FirstOrDefault() means that if the user typed a wood type correctly then returns the correct Wood object; otherwise, Nothing.
            selectedWood = woods.FirstOrDefault(Function(wood) String.Equals(wood.Type, selectedWoodType, StringComparison.OrdinalIgnoreCase))

            If (selectedWood IsNot Nothing) Then
                Exit While
            End If

            Console.WriteLine("Sorry.")

        End While

        Console.WriteLine("Thank you for selection, you have picked {0} at {1}", selectedWood.Type, selectedWood.Price)

        Return selectedWood

    End Function

    Public Function GetDrawerQtys() As Integer

        Dim drawerQty As Integer
        Dim rawQty As String = Nothing

        Do
            Console.Write("How many drawers do you want? ")
            rawQty = Console.ReadLine()
        Loop Until Int32.TryParse(rawQty, drawerQty)

        Return drawerQty

    End Function

    Public Function CalculatePrice(wood As Wood, quantity As Integer) As Integer
        Console.WriteLine("You have ordered a {0} with {1} drawers at {2} each.", wood.Type, quantity, wood.Price)
        Return wood.Price + 30 * quantity
    End Function

    Public Sub DisplayOrder(wood As Wood, quantity As Integer, basePrice As Integer)
        Console.WriteLine("Hello, you have purchased {0} {1} at {2} your total is {3}.", quantity, wood.Type, wood.Price, basePrice)
        Console.ReadLine()
    End Sub

End Class

然后你可以有一个模块,你可以使用这些类。

Module Module1

    Sub Main()

        Dim shop As New Shop()

        Dim selectedWood As Wood = shop.SelectWood()
        Dim quantity As Integer = shop.GetDrawerQtys()
        Dim basePrice As Integer = shop.CalculatePrice(selectedWood, quantity)

        shop.DisplayOrder(selectedWood, quantity, basePrice)

    End Sub

End Module

答案 1 :(得分:0)

在致电nameIndex后,您没有立即查看GetWoodTypes。 这将使您更进一步,但如果您不输入整数,GetDrawerQtys仍然存在问题。

   Shared Sub Main()

        Dim woodType() As String = {"Pine", "Oak", "Elm"}
        Dim woodPrice() As Integer = {100, 140, 200}
        Dim nameIndex As Integer = 0
        nameIndex = -1
        Dim Quantity As Integer = 0
        Dim basePrice As Integer = 0
        'Quantity = GetDrawerQtys()
        'basePrice = GetPrices(woodType, woodPrice, nameIndex, Quantity)

        nameIndex = GetWoodTypes(woodType, woodPrice)
        While nameIndex = -1
            Console.WriteLine("You have made the wrong selection, Please try Again")
            nameIndex = GetWoodTypes(woodType, woodPrice)
        End While

        Quantity = GetDrawerQtys()
        basePrice = GetPrices(woodType, woodPrice, nameIndex, Quantity)
        DisplayResults(Quantity, woodType, woodPrice, basePrice, nameIndex)    
    End Sub


   Public Function GetDrawerQtys()

        Dim validQty As Boolean = False
        Dim DrawerQty As Integer

        While Not validQty
            Console.WriteLine(" How Many Drawers do you want ")
            validQty = Int32.TryParse(Console.ReadLine(), DrawerQty)
        End While

        Return DrawerQty

    End Function