2015-10-18 101 views
-1

的Visual Basic如何改進此代碼以使其更簡潔高效?

這是一個程序我有written.The最後部分(如下所示)的代碼的最後部分顯示收據。但是,它有很多重複的代碼,所以我認爲它可以通過一些更好的代碼進行改進。但是,我不知道如何做到這一點。

那麼,你可以請改進代碼,使它更短,更簡潔(也許有循環?),並寫出改進的代碼。如果您還可以解釋改進的代碼,我將非常感激。

下面是在Visual Basic中的代碼,我想你改善:

Dim itemName (4) As String 
    Dim priceOfItem(4) As Decimal 
    Dim amountOrdered(4) As Decimal 
    'the "completePriceOfItem" array is simply the "priceOfItem" multipled by the "amountOrdered" but ,as this is only a... 
    '...part of my program, this has already been processed and assigned to the aray. 
    Dim completePriceOfItem(4) As Decimal 
    'setting up variables 
    Dim numberOfItems As Integer = 0 

    'final section of program where it displays the receipt 
    Console.WriteLine("Receipt:") 
    Console.WriteLine 
    If numberOfItems = 1 Then 
     Console.WriteLine(itemName(0) & ": " & Format (priceOfItem(0), "Currency") & " each" & ", " & amountOrdered(0) & " bought" & ", " & Format (completePriceOfItem(0), "Currency") & " in total") 
    ElseIf numberOfItems = 2 Then 
     Console.WriteLine(itemName(0) & ": " & Format (priceOfItem(0), "Currency") & " each" & ", " & amountOrdered(0) & " bought" & ", " & Format (completePriceOfItem(0), "Currency") & " in total") 
     Console.WriteLine(itemName(1) & ": " & Format (priceOfItem(1), "Currency") & " each" & ", " & amountOrdered(1) & " bought" & ", " & Format (completePriceOfItem(1), "Currency") & " in total") 
    ElseIf numberOfItems = 3 Then 
     Console.WriteLine(itemName(0) & ": " & Format (priceOfItem(0), "Currency") & " each" & ", " & amountOrdered(0) & " bought" & ", " & Format (completePriceOfItem(0), "Currency") & " in total") 
     Console.WriteLine(itemName(1) & ": " & Format (priceOfItem(1), "Currency") & " each" & ", " & amountOrdered(1) & " bought" & ", " & Format (completePriceOfItem(1), "Currency") & " in total") 
     Console.WriteLine(itemName(2) & ": " & Format (priceOfItem(2), "Currency") & " each" & ", " & amountOrdered(2) & " bought" & ", " & Format (completePriceOfItem(2), "Currency") & " in total") 
    ElseIf numberOfItems = 4 Then 
     Console.WriteLine(itemName(0) & ": " & Format (priceOfItem(0), "Currency") & " each" & ", " & amountOrdered(0) & " bought" & ", " & Format (completePriceOfItem(0), "Currency") & " in total") 
     Console.WriteLine(itemName(1) & ": " & Format (priceOfItem(1), "Currency") & " each" & ", " & amountOrdered(1) & " bought" & ", " & Format (completePriceOfItem(1), "Currency") & " in total") 
     Console.WriteLine(itemName(2) & ": " & Format (priceOfItem(2), "Currency") & " each" & ", " & amountOrdered(2) & " bought" & ", " & Format (completePriceOfItem(2), "Currency") & " in total") 
     Console.WriteLine(itemName(3) & ": " & Format (priceOfItem(3), "Currency") & " each" & ", " & amountOrdered(3) & " bought" & ", " & Format (completePriceOfItem(3), "Currency") & " in total") 
    ElseIf numberOfItems = 5 Then 
     Console.WriteLine(itemName(0) & ": " & Format (priceOfItem(0), "Currency") & " each" & ", " & amountOrdered(0) & " bought" & ", " & Format (completePriceOfItem(0), "Currency") & " in total") 
     Console.WriteLine(itemName(1) & ": " & Format (priceOfItem(1), "Currency") & " each" & ", " & amountOrdered(1) & " bought" & ", " & Format (completePriceOfItem(1), "Currency") & " in total") 
     Console.WriteLine(itemName(2) & ": " & Format (priceOfItem(2), "Currency") & " each" & ", " & amountOrdered(2) & " bought" & ", " & Format (completePriceOfItem(2), "Currency") & " in total") 
     Console.WriteLine(itemName(3) & ": " & Format (priceOfItem(3), "Currency") & " each" & ", " & amountOrdered(3) & " bought" & ", " & Format (completePriceOfItem(3), "Currency") & " in total") 
     Console.WriteLine(itemName(4) & ": " & Format (priceOfItem(4), "Currency") & " each" & ", " & amountOrdered(4) & " bought" & ", " & Format (completePriceOfItem(4), "Currency") & " in total") 
    End If 
    Console.ReadLine 
+0

此外,我想指出,我是一名初學者程序員。 –

+0

看起來很像你在一門入門編程課程中遇到的問題。試圖讓我們做你的功課? –

+0

不,那是我自己的問題,誠實。 –

回答

0

您可以使用一個簡單的for循環。用以下替換您if邏輯...

For index As Integer = 0 To numberOfItems - 1 
    Console.WriteLine(itemName(index) & ": " & Format (priceOfItem(index), "Currency") & " each" & ", " & amountOrdered(index) & " bought" & ", " & Format (completePriceOfItem(index), "Currency") & " in total") 
Next 

變量index遞增每個迴路這意味着你可以取代你012等變量名稱「index」摳數據在正確的索引。

循環將運行numberOfItems變量中定義的次數。

+1

它應該不是0到'numberofItems - 1'? – haraman

+0

是的,它應該。更正了 – Turnip

+0

謝謝。這對我來說非常好。 –

1

下面是我如何使這段代碼更好。

我會創建幾個類。一個用於Order,以及一個用於OrderLine

Public Class Order 

    Public Property Lines As List(Of OrderLine) 

    Public Sub New() 
     Lines = New List(Of OrderLine)() 

    End Sub 

End Class 

Public Class OrderLine 

    Public Property itemName As String 


    Public Property priceOfItem As Decimal 

    Public Property amountOrdered As Decimal 

    Public ReadOnly Property CompletePriceOfItem() As Decimal 
     Get 
      Return priceOfItem * amountOrdered 
     End Get 
    End Property 

End Class 

然後我會改變Main這樣的代碼:

Dim Orders As List(Of Order) = New List(Of [Order])() 

'Create a new Order, and add OrderLine to it 
Dim o As New Order() 
Dim ol As OrderLine 

ol = New OrderLine() 
ol.itemName = "Item1" 
ol.priceOfItem = 10.99 
ol.amountOrdered = 3 

o.Lines.Add(ol) 

Orders.Add(o) 

'final section of program where it displays the receipt 
Console.WriteLine("Receipt:") 
Console.WriteLine() 

For Each ord As Order In Orders 

    For Each ordline As OrderLine In ord.Lines 
     Console.WriteLine(ordline.itemName & ": " & Format(ordline.priceOfItem, "Currency") & " each" & ", " & ordline.amountOrdered & " bought" & ", " & Format(ordline.completePriceOfItem, "Currency") & " in total") 

    Next 
Next 

Console.ReadLine() 

所以,而不是硬編碼的項目數,我用List(Of...)這可以根據需要增長。另外,使用OrderLine的類不需要單獨的數組。

最後,我沒有爲CompletePriceOfItem設置單獨的數組,而是讓類的屬性處理計算。

這只是一個粗略的概念,但我認爲你會明白。

乾杯

+0

感謝您的幫助。 –

+0

我看到你更喜歡另一種解決方案。你能解釋爲什麼嗎?我的解決方案太困難了,還是您發現問題?我想在回答問題時做得更好,所以您的反饋將幫助我提高答案的質量。 –

+0

由於我只是一個初學者,我並不真正瞭解你的代碼(例如,我不知道公共,私人和類的東西是什麼)。但是,它看起來像你在答案中付出了很多努力,所以我仍然感謝你的幫助。 :) –

相關問題