这是我的代码:
当dblprevious
大于dblcurrent
时,它会调用invalidinput,但它会显示负输出,而我知道它必须简单
Public Class Form1
'names the the class veriables
Dim dblCurrent As Double
Dim dblPervious As Double
Private Sub invalidinput()
Const Message As String = "check your input"
Const title As String = " Attention!"
Const buttons = MessageBoxButtons.OK
MessageBox.Show(Message, title, buttons)
End Sub
Private Sub Total()
Dim dblTotalP As Double
Dim dblTotalG As Double
Const dblPerGC As Double = 5.15
Const dbltMinC As Double = 19.69
'stores the veriables
Double.TryParse(txtCurrentreading.Text, dblCurrent)
Double.TryParse(txtPerivousreading.Text, dblPervious)
dblTotalG = dblCurrent - dblPervious
dblTotalP = dblTotalG / 1000 * dblPerGC
If dblTotalP < dbltMinC Then
dblTotalP = dbltMinC
End If
'displays totals
lblTotalG.Text = dblTotalG
lblTotalP.Text = dblTotalP.ToString("C2")
End Sub
Private Sub bntExit_Click(sender As Object, e As EventArgs) Handles bntExit.Click
Me.Close()
End Sub
Private Sub txtCurrentreading_keypress(sender As Object, e As KeyPressEventArgs) Handles txtCurrentreading.KeyPress
'allows text box to accept only numbers and the back space key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub txtPerivousreading_keypess(sender As Object, e As KeyPressEventArgs) Handles txtPerivousreading.KeyPress
'allows text box to accept only numbers and the back space key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub bntCalculate_Click(sender As Object, e As EventArgs) Handles bntCalculate.Click
'determines which calculations to do
If dblCurrent >= dblPervious Then
Call Total()
ElseIf dblPervious < dblCurrent Then
Call invalidinput()
End If
End Sub
End Class
答案 0 :(得分:0)
当dblprevious大于时,它意味着调用invalidinput dblcurrent
变化:
If dblCurrent >= dblPervious Then
Call Total()
ElseIf dblPervious < dblCurrent Then
Call invalidinput()
End If
要:
If dblCurrent >= dblPervious Then
Call Total()
Else
Call invalidinput()
End If
您在If dblPervious < dblCurrent Then
的else块中的原始支票直接与您的要求相矛盾!如果&#34; dblCurrent&#34;不大于或等于&#34; dblPervious&#34;,然后&#34; dblPervious&#34;必须大于&#34; dblCurrent&#34;,从而允许我们完全删除第二次检查。