2015-12-09 35 views
0

在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 = 
+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 

希望这有助于您!