0

嗨,我被分配了一项家庭作业来创建以下程序: 手机公司 Cell4U 确定他们的手机费用如下:客户为整分钟的通话支付 R2.05,每多出一秒支付 2c。他们还为每条短信支付 40 美分,但客户每满 30 分钟通话可获得 2 条免费短信。每发送 30 条短信,客户还可免费获得 1 条短信。

为公司工作的所有员工都会获得一部免费手机作为福利,他们的账户每月最多可支付 R800 兰特。该公司要求每月报告所有员工的手机账单以及他们在员工手机津贴上花费的总金额。

该计划必须提供以下内容:

• 呼叫到期金额和短信到期金额必须在子过程中计算。这必然意味着子程序必须将秒转换为分钟和秒,并且这些值以及空闲短信的数量也可用于主程序。• 必须调用第二个子程序来计算员工的总账单并累计公司需要为手机津贴支付的总金额。此子程序还必须使用布尔参数向主程序指示公司是否将支付该员工的全部金额或仅支付 R800。• 所有输出必须显示在主程序中(按钮单击事件)。• 任意数量的员工。一个空的员工编号将表示输入的结束(或者用户可以单击输入框中的取消按钮)。输入最后一个数据后,总金额必须如图 5 所示显示。请记住,如果帐户超过 R800,则公司仅向账单贡献 R800,并且总额中必须反映 R800。

我已经编写了以下代码,但返回的值为零

Option Strict On
Option Explicit On

Public Class TheCellPhoneCompany

Private Sub CalcMinAndSec(ByVal intLengthCallInSec As Integer, ByVal intCallSec As Integer, ByVal intCallMin As Integer, ByVal intNumMsgs As Integer, ByVal intNumFreeMsgs As Integer, ByVal decCallCost As Decimal, _
                          ByVal decSmsCost As Decimal, ByVal RatePerMin As Decimal, ByVal RatePerSec As Decimal, ByVal RatePerSms As Decimal)

    intCallMin = (intLengthCallInSec \ 60I)
    intCallSec = ((intLengthCallInSec Mod 60I))
    intNumFreeMsgs = ((intCallMin - (intCallMin Mod 30)) \ 15) + ((intNumMsgs - (intNumMsgs Mod 30I) \ 30I))
    decCallCost = (intCallMin * RatePerMin) + (intCallSec * RatePerSec)
    decSmsCost = (intNumMsgs - intNumFreeMsgs) * RatePerSms

End Sub

Private Sub CalcTotals(ByVal decCallCost As Decimal, ByVal decSmsCost As Decimal, ByVal decTotCost As Decimal, ByVal decAmtPaid As Decimal, ByVal decFinalAmtPaid As Decimal, ByVal PaidByComp As Decimal)

    Dim blnPaidByComp As Boolean = decTotCost > 800I

    decTotCost = decCallCost + decSmsCost
    If blnPaidByComp = True Then
        decAmtPaid = PaidByComp
    Else
        decAmtPaid = decTotCost
    End If

    decFinalAmtPaid += decAmtPaid

End Sub

Private Sub btnEnterData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterData.Click

    Dim strEmpCode, strLengthCallInSec, strNumOfSms As String
    Dim intNumMsgs, intNumFreeMsgs, intCallMin, intCallSec, intEmpCode, intLengthCallInSec As Integer
    Dim decCallCost, decSmsCost, decTotCost, decAmtPaid, decFinalAmtPaid As Decimal
    Dim intEmpNum As Integer = 1

    Const RatePerMin As Decimal = 2.05D
    Const RatePerSec As Decimal = 0.02D
    Const RatePerSms As Decimal = 0.4D
    Const PaidByComp As Decimal = 800D

    Do

        strEmpCode = InputBox("Please enter employee number", "Employee " & intEmpNum)
        Integer.TryParse(strEmpCode, intEmpCode)
        If intEmpCode <> 0 Then


            strLengthCallInSec = InputBox("Enter total calls for the month in seconds", "Employee " & intEmpNum)
            Integer.TryParse(strLengthCallInSec, intLengthCallInSec)

            If intLengthCallInSec > 0 Then
                strNumOfSms = InputBox("Enter total number of SMS's sent for the month", "Employee " & intEmpNum)
                Integer.TryParse(strNumOfSms, intNumMsgs)

                If intNumMsgs > 0 Then

                Else
                    MessageBox.Show("Invalid value - the number of SMS's must be a positive integer", "Error - please correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                End If

            Else
                MessageBox.Show("Invalid value - the seconds must be a positive integer", "Error - please correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
            End If

        Else
        End If

        Call CalcMinAndSec(intLengthCallInSec, intCallSec, intCallMin, intNumMsgs, intNumFreeMsgs, decCallCost, decSmsCost, RatePerMin, RatePerSec, RatePerSms)

        Call CalcTotals(decCallCost, decSmsCost, decTotCost, decAmtPaid, decFinalAmtPaid, PaidByComp)

        lstCellPhones.Items.Add("Employee: " & intEmpCode)
        lstCellPhones.Items.Add("Calls: " & intCallMin & " minutes and " & intCallSec & " seconds")
        lstCellPhones.Items.Add("Number of SMS messages: " & intNumMsgs & " (" & intNumFreeMsgs & ")")
        lstCellPhones.Items.Add("Cost for calls: R" & decCallCost.ToString("N2"))
        lstCellPhones.Items.Add("Cost for sms messages: R" & decSmsCost.ToString("N2"))
        lstCellPhones.Items.Add("Total cost: R" & decTotCost.ToString("N2"))
        lstCellPhones.Items.Add("Paid by company: R" & decAmtPaid.ToString("N2"))
        lstCellPhones.Items.Add("")

        intEmpNum = intEmpNum + 1


    Loop Until intEmpCode = 0

    lstCellPhones.Items.Add("Total amount paid by company for cell phones = R" & decFinalAmtPaid.ToString("N2"))




End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    lstCellPhones.Items.Clear()

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

    Me.Close()

End Sub
End Class
4

1 回答 1

4

1) 通常不将值传递给 sub 以更改这些值。相反,如果您更改变量的范围,decCallCost以便它们对您的所有代码“可见”,那么您将通过某种方式使其工作。

2)上线

decSmsCost = (intNumMsgs - intNumFreeMsgs) * RatePerSms

如果 intNumFreeMsgs > intNumMsgs 会发生什么?

3)虽然您通常不会这样做,并且由于这是家庭作业,这将是一个坏主意,但您可以更改作为参数传递给子的变量:

Module Module1

    Sub x(ByRef n As Integer)
        n = 5
    End Sub

    Sub Main()
        Dim a As Integer = 1
        x(a)
        Console.WriteLine(a) ' outputs 5
        Console.ReadLine()
    End Sub

End Module

以防万一您在某处看到它并想知道发生了什么。

于 2012-09-13T18:03:28.760 回答