Visual Basic Vending Machine

一世执手 提交于 2019-12-13 16:13:12

问题


I have started work on a Vending Machine code in Visual Basic. It works so far but I wanted it to come up with an error message when you can't afford something. It's still fairly basic with no items on it yet but wanted this to be out of the way before I start with anything else. Currently it's coming up with an error for the "Then" statement and don't know how to fix it. Again im very new to it and aprecate any advise. Thanks.

Public Class Form1

Private Sub BuyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuyButton.Click

    Dim Cost, Amount, Change As Decimal
    Dim Pennies, Pounds As Integer
    Dim msg As String

    Cost = Decimal.Parse(CostTextBox.Text)
    Amount = Decimal.Parse(AmountTextBox.Text)
    Change = (Amount - Cost) * 10

    Pounds = Change \ 10
    Change = Change Mod 10

    Pennies = Change \ 10
    Change = Change Mod 10

    If Change = "Your change is: -" Then

        msg = "You don't have enough Money"

    Else

        msg = "Your change is: " & Change & vbNewLine
        msg += "Pennies: " & Pounds & vbNewLine
        msg += "Pounds: " & Pennies & vbNewLine

    End If

    ChangeLabel.Text = msg

End Sub
End Class

Sulution I have used: Public Class Form1

Private Sub BuyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuyButton.Click

    Dim Cost, Amount, Change As Decimal
    Dim Pennies, Pounds As Integer
    Dim msg As String

    Cost = Decimal.Parse(CostTextBox.Text)
    Amount = Decimal.Parse(AmountTextBox.Text)
    Change = (Amount - Cost)

    If Change < 0 Then

        msg = "You don't have enough Money"
        AmountTextBox.Text = Change + Cost

    Else

        AmountTextBox.Text = Change
        Pounds = Math.Floor(Change)
        Pennies = (Change - Pounds) * 100

        msg = "Your change is: " & Change.ToString("##.00") & Environment.NewLine
        msg += "Pounds: " & Pounds & vbNewLine
        msg += "Pennies: " & Pennies & vbNewLine

    End If
    ChangeLabel.Text = msg
End Sub

回答1:


First, to check if there's enough money, you check if change is less than 0 - If Change <0 Then .... Second, you are changing Change with Change = Change mod 10, so at the end you won't get correct result. Third, it is easier to make Change a integer. To get last two digits of change(pennies), you should use Pennies=Change mod 100, and to get other digits(pounds), you should use Pounds=Change\100. Fourth, you swapped pounds and pennies while creating message. And last, you only need to split the change to pounds and pennies if there is change.

Public Class Form1

Private Sub BuyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuyButton.Click

    Dim Amount, Change As Decimal
    Dim Cost, Pennies, Pounds As Integer
    Dim msg As String

    Cost = Decimal.Parse(CostTextBox.Text)
    Amount = Decimal.Parse(AmountTextBox.Text)
    Change = (Amount - Cost) * 100

    If Change < 0 Then

        msg = "You don't have enough Money"

    Else

        Pounds = Change \ 100
        Pennies = Change Mod 100
        msg = "Your change is: " & Change & vbNewLine
        msg += "Pounds: " & Pounds & vbNewLine
        msg += "Pennies: " & Pennies & vbNewLine

    End If

    ChangeLabel.Text = msg

End Sub
End Class



回答2:


If I were you I'd look at streamlining this a bit. Stop dividing by 10 and multiplying by 10 - it's not helpful to your cause. Go for something like this.

Dim Cost, Amount, Change As Decimal
    Dim Pennies, Pounds As Integer
    Dim msg As String

    Cost = Decimal.Parse(CostTextBox.Text)
    Amount = Decimal.Parse(AmountTextBox.Text)
    Change = (Amount - Cost)  --Work in pounds

    If Change < 0 Then

        msg = "You don't have enough Money"

    Else

        Pounds = Math.Floor(Change)
        Pennies = (Change - Pounds) * 100  --Multiply pounds by 100 to get pence

        --Environment.NewLine is the preferred way to insert a newline which will
        --always be correct for the environment.
        msg = "Your change is: " & Change.ToString("##.00") & Environment.NewLine --Note the .ToString("##.00") - this tells VB to convert the number to a string and show two decimal places
        msg += "Pounds: " & Pounds.ToString() & Environment.NewLine
        msg += "Pennies: " & Pennies.ToString() & Environment.NewLine

    End If

    ChangeLabel.Text = msg

Avoid implicit conversions at all costs. Yes, Pounds = Change / 10 works, but it is an implicit narrowing and may not work how you expect it to. The reason we use Math.Floor here is because we want the integer portion of the decimal and that's what Math.Floor does - gives you the largest whole number that is equal to or less than the supplied value.

Also note the use of the Change.ToString - all intrinsic types are derived from the super-type Object which means they implement the method ToString. This will give you an explicitly converted string representation of the value of the object to which you can apply formatting by supplying a mask (the "##.00#"bit in the parentheses). Have a look at the MSDNpage on the subject to see more about applying custom formats to strings.




回答3:


If Change = "Your change is: -" Then

the change value should equate to a value, and not to a string this will need to be changed to something like

If change < 0 Then

normally when i am contatinating string i use the & operator,in not too sure if the =+ version works (i've never tried it)

msg = msg &  "Pennies: " & Pounds & vbNewLine
msg = msg & "Pounds: " & Pennies & vbNewLine


来源:https://stackoverflow.com/questions/23408441/visual-basic-vending-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!