计算条件成本

时间:2015-12-09 15:18:32

标签: computer-science

在Visual Basic中有一个示例告诉我,如果复制中心在前100个副本上每个副本收取5美分,在100个副本后每个附加副本收取3美分,则必须计算副本的成本,然后显示文本框中的费用。

这是我到目前为止所拥有的

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim firsthun As Double
        Dim plushun As Double
        If firsthun <= 100 Then '5 cents per copy'

        End If

        If plushun >= 100 Then 'add 3 cents more'

        End If
        TextBox2.Text = 

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim copies As Integer = TextBox1.Text
    Dim cost As Double = Nothing

    If copies <= 100 Then '5 cents per copy'
        cost = copies * 0.05
    ElseIf copies > 100 Then 'add 3 cents more'
        cost = 5 + (copies - 100) * 0.03 '5$ for the first 100, plus $0.03 for every copy after 100
    End If

    TextBox2.Text = Math.Round(cost, 2).ToString
End Sub

在这里,我创建了一个文本框TextBox1,供用户输入他们想要的副本数量。当他们点击Button1时,它会计算该值,然后将其输出到TextBox2,四舍五入到小数点后两位。

请注意,如果用户将整数以外的任何值输入TextBox1,程序将崩溃,因为它无法将输入的值转换为整数。您可能需要Try / Catch语句,如下所示:

Dim copies As Integer

Try
    copies = CInt(TextBox1.Text)
Catch ex As Exception
    MessageBox.Show("Please enter a valid amount!")
End Try

希望这有帮助!