Visual Basic自动售货机

时间:2014-05-01 13:14:27

标签: vb.net message

我已开始使用Visual Basic中的自动售货机代码。它到目前为止工作,但我希望它能够提供一些错误消息,当你无法负担得起的东西。它仍然相当基础,但还没有任何项目,但是在我开始其他任何事情之前,我希望这一点不受影响。目前它正在为"然后"出现错误。声明并且不知道如何解决它。我再次非常新,并建议任何建议。感谢。

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: 公共类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

3 个答案:

答案 0 :(得分:1)

首先,要检查是否有足够的钱,请检查更改是否小于0 - If Change <0 Then ...。其次,您正在使用Change更改Change = Change mod 10,因此最终您无法获得正确的结果。第三,更改整数更容易。要获得最后两位数的变化(便士),您应该使用Pennies=Change mod 100,并获得其他数字(磅),您应该使用Pounds=Change\100。第四,你在创造信息的同时换掉了磅和便士。最后,如果有变化,你只需要将变化分成磅和便士。

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

答案 1 :(得分:1)

如果我是你,我会考虑简化这一点。停止除以10并乘以10 - 它对你的事业没有帮助。去做这样的事情。

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的作用 - 为您提供等于或小于提供值的最大整数

还要注意Change.ToString的使用 - 所有内在类型都是从超类型Object派生的,这意味着它们实现了方法ToString。这将为您提供一个显式转换的字符串表示形式的对象值,您可以通过提供掩码(括号中的"##.00#"位)来应用格式。 Have a look at the MSDNpage on the subject了解有关将自定义格式应用于字符串的更多信息。

答案 2 :(得分:0)

If Change = "Your change is: -" Then

更改值应该等于一个值,而不是一个字符串,这需要更改为

If change < 0 Then

通常当我收缩字符串时我使用&运算符,不太确定= +版本是否有效(我从未尝试过)

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