【问题标题】:Drink Vending Machine Code饮料自动售货机代码
【发布时间】:2017-03-30 22:58:55
【问题描述】:

您好 Stackflow 社区, 我目前正在上 Visual Basic 课程,我需要一些帮助。我正在创建一个使用数组模拟饮料自动售货机的程序。说明如下: 创建一个模拟软饮料自动售货机的应用程序。该应用程序应让用户选择以下软饮料之一:

可乐(每个 1.00 美元) 根啤酒(每个 1.00 美元) 柠檬酸橙苏打水(每个 1.00 美元) 葡萄汽水(每个 1.50 美元) 奶油苏打水(每个 1.50 美元)

当应用程序启动时,自动售货机将有 20 种软饮料。每次用户选择饮料时,应用程序应从所选饮料的数量中减去 1。它还应该更新并显示总销售额。如果用户选择了已售罄的饮料,则应显示一条消息指示已售罄。

在应用程序的代码中,创建一个包含以下数据字段的结构:

饮品名称 饮料费用 机内饮品数量

程序应该创建一个包含五个结构对象的数组。数组的每个元素都应保存特定类型软饮料的数据。

我不知道如何让我的按钮减少数量标签,然后将每次点击添加到标签总数中。请帮忙。

Public Class Form1
' Class-level declarations
Const intMAX_SUBSCRIPT As Integer = 4               ' Upper subscript
Dim strProdNames(intMAX_SUBSCRIPT) As String        ' Product Names
Dim decPrice(intMAX_SUBSCRIPT) As Decimal           ' Drink Price
Dim intProdNums(intMAX_SUBSCRIPT) As Integer        ' Product Numbers

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Initialize the arrays with the product data.
    InitArrays()
End Sub

Private Sub InitArrays()
    ' Initialize the arrays.

    ' First Product
    strProdNames(0) = "Cola"
    decPrice(0) = 1D
    intProdNums(0) = 20

    ' Second Product
    strProdNames(1) = "Root Beer"
    decPrice(1) = 1D
    intProdNums(1) = 20

    ' Third Product
    strProdNames(2) = "Lemon Lime"
    decPrice(2) = 1D
    intProdNums(2) = 20

    ' Fourth Product
    strProdNames(3) = "Grape Soda"
    decPrice(3) = 1.5D
    intProdNums(3) = 20

    ' Fifth Product
    strProdNames(4) = "Cream Soda"
    decPrice(4) = 1.5D
    intProdNums(4) = 20

End Sub

Private Sub btnCola_Click(sender As Object, e As EventArgs) Handles btnCola.Click
    Dim intCount As Integer = 20        ' Loop Counter

    intCount -= 1                       ' Decrement drink count

    If intCount > 0 Then
        lblTotal.Text = decPrice(0).ToString("c")
    End If

End Sub

【问题讨论】:

    标签: vb.net winforms visual-studio-2015


    【解决方案1】:

    您需要减少数组中的可乐计数。

     Private Sub btnCola_Click(sender As Object, e As EventArgs)
            If intProdNums(0) > 0 Then
                intProdNums(0)-=1  ' decrement cola drink count
            End If
    
            ' show results here...
        End Sub
    

    【讨论】:

      猜你喜欢
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多