因此,我尝试通过添加或减去用户在amountTextBox中放置的金额来计算帐户的总金额,具体取决于它是什么(存款被添加到帐户总计,减去支票,以及减去服务费)。但是,当我在Visual Studio 2010中执行此操作时(因为我需要使用的程序),减去类型的数量显示为负数,而不是仅减去总数。它不会创建一个存款总额,然后从中减去;它只是从零减去并增加到零。我知道我的代码是错误的,但我不知道如何解决它或写什么来修复它。我还需要if / else语句才能使其正确。
Public Class Form1
Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
'Clear all textboxes and total variable
amountTextBox.Clear()
totalTextBox.Clear()
End Sub
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
'Close the program
Me.Close()
End Sub
Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printButton.Click
'Print the program
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
PrintForm1.Print()
End Sub
Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
'Create variables
Dim totalAmount As Decimal
Dim depositAmount As Decimal
Dim checkAmount As Decimal
Dim serviceAmount As Decimal
'Create if/else statements to give variables amounts
If depositRadio.Checked Then
depositAmount = amountTextBox.Text
ElseIf checkRadio.Checked Then
checkAmount = amountTextBox.Text
ElseIf serviceChargeRadio.Checked Then
serviceAmount = amountTextBox.Text
End If
'Calculate total
totalAmount = depositAmount - checkAmount - serviceAmount
totalTextBox.Text = totalAmount
End Sub
结束班
答案 0 :(得分:0)
如果仔细观察,您会发现自己从未向totalTextBox
添加任何内容 - 只需将其替换为amountTextBox
的有符号值 - 因此显而易见的解决方案是将最后一行更改为:
totalTextBox.Text = totalTextBox.Text + (depositAmount - checkAmount - serviceAmount)
string
和decimal
之间使用隐式转换不是一个好主意(如果用户输入"shitton of money"
这样的格式错误的数字会怎么样?使用{{1} }或TryParse
进行异常处理,以理智的方式处理格式错误的输入。Parse
)一个干净的代码仙女去世