的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
此外,我想指出,我是一名初學者程序員。 –
看起來很像你在一門入門編程課程中遇到的問題。試圖讓我們做你的功課? –
不,那是我自己的問題,誠實。 –