水费账单计算器(VB.NET)问题

时间:2014-10-24 03:06:47

标签: vb.net

我在用VB编写的Water Bill计划中遇到了一些问题。

基本上我要做的是创建一个计算客户水费的应用程序。用户将输入当前的仪表读数和之前的仪表读数。应用程序应计算并显示所用水的加仑数和水的总费用。

水费为每1000加仑1.75美元,或每加仑0.00175美元。

首先,我最令人沮丧的问题是我收到此错误The class waterBill can be designed, but is not the first class in the file.我正在复制并粘贴论坛中的代码,以了解它是如何工作的。我无意保留代码,我只是测试它,当我试图恢复到原始代码时,此错误仍然存​​在。当我删除复制的代码时,我不明白错误是如何存在的。

第二件事是我无法让我的计算器进行任何计算。每按一次计算,它显示0.这是我的VB代码......

Public Class waterBill

Dim currentReading As Integer
Dim previousReading As Integer
Dim gallonsUsed As Integer
Dim totalCharge As Decimal

Private Sub calculateCharge()
    If (currentReading >= previousReading) Then
        gallonsUsed = currentReading - previousReading
        totalCharge = gallonsUsed * 0.00175
        ElseIf (currentReading < previousReading) Then
            MessageBox.Show("Current reading must be greater than or equal to previous reading", "Water Bill Calculator", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub displayCharge()
        txtGallonsUsed.Text = gallonsUsed
        txtTotalCharge.Text = totalCharge
    End Sub

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Call calculateCharge()
        Call displayCharge()
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

非常感谢任何帮助。希望我的小絮絮很有道理。

1 个答案:

答案 0 :(得分:0)

'Declaring a integer variable will make it as 0 by default, you need to set the value.
Dim currentReading As Integer
Dim previousReading As Integer
Dim gallonsUsed As Integer
Dim totalCharge As Decimal

Private Sub calculateCharge()
    'I suppose you're missing two input control to set the value of currentReading and previousReading 
    previousReading = CInt(txtPrevious.Text)
    currentReading = CInt(txtCurrent.Text)

    If (currentReading >= previousReading) Then
        gallonsUsed = currentReading - previousReading
        totalCharge = gallonsUsed * 0.00175
    ElseIf (currentReading < previousReading) Then
        MessageBox.Show("Current reading must be greater than or equal to previous reading", "Water Bill Calculator", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
End Sub

Private Sub displayCharge()
    txtGallonsUsed.Text = gallonsUsed
    txtTotalCharge.Text = totalCharge
End Sub

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Call calculateCharge()
    Call displayCharge()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub