2015-10-18 85 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

由于我只是一个初学者,我并不真正了解你的代码(例如,我不知道公共,私人和类的东西是什么)。但是,它看起来像你在答案中付出了很多努力,所以我仍然感谢你的帮助。 :) –

相关问题