计算几项具有税收和折扣的成本

  • 本文关键字:几项 计算 vb.net
  • 更新时间 :
  • 英文 :


任何人都可以帮助完成这项学校任务

任务是向用户询问项目和项目的成本,直到他们选择停止。然后将所有成本组合起来,从2个随机选择的物品中获得20%的增值税和10%的折扣。

这是我到目前为止的代码(我有2个按钮和一个listbox)

Public Class Form1
    Dim CurrentA As Integer
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Items(CurrentA) As String
        Dim Coins(CurrentA) As Single
        Dim Stay As String
        CurrentA = 0
        Do Until CurrentA = 20
            Items(CurrentA) = InputBox("Please Enter The Item")
            Coins(CurrentA) = InputBox("Please Enter The Cost Of The Item")
            Stay = InputBox("Type Yes If More Items or Type No if no More")
            Stay = Stay.ToLower
            If Stay = "yes" Then
            End If
            If Stay = "no" Then
                Exit Do
            End If
            ListBox1.Items.Add(Items(CurrentA) & " " & Coins(CurrentA))
            CurrentA += 1
        Loop
    End Sub
End Class

首先,对您提出的代码进行了一些评论。

Dim CurrentA As Integer
'An Integers default value is zero, I don't see why this is a class level variable
'always declare variables with as narrow a scope as possible
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim Items(CurrentA) As String 'Declares an Array of Type String with an Upper Bound of 0
    'Upper Bound is the highest index in the array
    'Arrays start with index 0
    'So your array will have 1 element at index 0
    Dim Coins(CurrentA) As Single
    Dim Stay As String
    CurrentA = 0 'unnecessary because the default of CurrentA is already 0, but OK for clarity because it could have been changed elsewhere
    'This is behaving like a console application with the code repeating in a loop.
    'In Windows Forms it would be more likely to do this in a button click event (btnAddItem)
    Do Until CurrentA = 20
        'On the first iteration CurrentA = 0
        'On the second iteration CurrentA = 1 - this exceeds the size of your array
        'and will cause an index out of range error
        Items(CurrentA) = InputBox("Please Enter The Item")
        'With Option Strict on you must change the input to a Single
        Coins(CurrentA) = CSng(InputBox("Please Enter The Cost Of The Item"))
        Stay = InputBox("Type Yes If More Items or Type No if no More")
        Stay = Stay.ToLower 'Good! The user might no follow directions exactly
        If Stay = "yes" Then
            'This is kind of silly because it does nothing
        End If
        'Lets say I say no on the first iteration
        'This avoids the index out of range error but
        'nothing is added to the list because you Exit the loop
        'before adding the item to the ListBox
        If Stay = "no" Then
            Exit Do
        End If
        ListBox2.Items.Add(Items(CurrentA) & " " & Coins(CurrentA))
        CurrentA += 1
    Loop
End Sub

我们可以使用数组,但不知道将添加多少个项目,意味着使数组比所需的数组更大,或者在每个添加过程中都使用Redim Presise。一个更好的选择是(T)列表。它们有点像阵列,但我们可以添加无redim的物品。

Private lstCost As New List(Of Single)
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    'Pretend this button is called btnAdd, and you have 2 test boxes
    lstCost.Add(CSng(TextBox2.Text))
    'The $ introduces an interpolated string. It is a step up form String.Format
    ListBox2.Items.Add($"{TextBox1.Text} - {CSng(TextBox2.Text):C}") 'The C stands for currency
    TextBox1.Clear()
    TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'Pretend this button is called btnTotal
    'Dim total As Single = (From cost In lstCost
    '                       Select cost).Sum
    Dim total As Single = lstCost.Sum
    Label1.Text = total.ToString("C") 'C for Currency
End Sub

最新更新