的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
此外,我想指出,我是一名初学者程序员。 –
看起来很像你在一门入门编程课程中遇到的问题。试图让我们做你的功课? –
不,那是我自己的问题,诚实。 –