计算含税和折扣的几件商品的成本

时间:2019-02-19 23:11:17

标签: vb.net

任何人都可以帮我完成这个学校任务

任务是向用户询问物品和物品费用,直到他们选择停止为止。然后结合所有费用,从2个随机选择的商品中分别扣除20%的增值税和10%的折扣。

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

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

1 个答案:

答案 0 :(得分:0)

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

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 Preserve。更好的选择是List(Of 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