Visual Basic自动贩卖机 [英] Visual Basic Vending Machine

查看:120
本文介绍了Visual Basic自动贩卖机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始在Visual Basic中处理自动售货机代码.到目前为止,它仍然有效,但是当您负担不起某些费用时,我希望它给出错误消息.它仍然是相当基本的,上面没有任何物品,但是在我开始任何其他事情之前,希望它能顺利进行.当前,它为"Then"语句提出了一个错误,并且不知道如何解决它.再次对它来说是非常新的,并建议不要提出任何建议.谢谢.

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

我曾经使用过的解决方案: 公共课程表格1

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

推荐答案

如果我是我,我会考虑简化一下.停止除以10再乘以10-这对您的事业没有帮助.追求这样的东西.

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

不惜一切代价避免隐式转换.是的,Pounds = Change / 10可以工作,但是它隐含地缩小了,可能无法按您期望的那样工作.之所以使用Math.Floor是因为我们想要小数的整数部分,而这正是Math.Floor的作用-为您提供等于或小于提供值的最大整数.

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.

还请注意Change.ToString的使用-所有固有类型都从超类型Object派生,这意味着它们实现了方法ToString.这将为您提供对象值的显式转换的字符串表示形式,您可以通过提供掩码(括号中的"##.00#"位)将格式应用于该对象. 看看有关该主题的MSDN页面查看有关将自定义格式应用于字符串的更多信息.

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.

这篇关于Visual Basic自动贩卖机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆