编写此代码的另一种方法是什么 [英] What is an alternate way to write this code

查看:78
本文介绍了编写此代码的另一种方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 ' 本地变量声明 
Dim dblLengthOfStay As Double ' 保留住院天数
Dim dblMedicationCharges As Double ' 持有药物费用
Dim dblSurgicalCharges As Double ' 保留手术费
Dim dblLabFees As Double ' 保留实验室费用
Dim dblPhysicalRehabCharges As Double ' 保存身体康复费用
Dim dblStayCharges As Double ' 保留住院费用的基本费用
Dim dblMiscCharges 正如 Double ' 保持总数miscelaneous charge
Dim d blTotalCharges As Double ' 保留总费用

' 获取作为用户输入的停留时间。
' 验证该值是否为正数数字。
如果 Double .TryParse(txtLengthOfStay.Text,dblLengthOfStay)< span class =code-keyword>和
dblLengthOfStay> = 0 0 然后

' 从用户那里获得药物费用。
' 验证该值是正实数。
如果 Double .TryParse(txtMedicationCharges.Text,dblMedicationCharges)< span class =code-keyword>和
dblMedicationCharges> = 0 0 然后

' 从用户那里获取手术费用。
' 验证该值是否为正实数。
如果 Double .TryParse(txtSurgicalCharges.Text,dblSurgicalCharges)
dblSurgicalCharges> = 0 0 然后

' 将实验室费用作为用户的输入。
' 验证该值为正实数。
如果 Double .TryParse(txtLabFees .Text,dblLabFees)
dblLabFees> = 0 0 然后

' 从用户那里获得物理康复费用。
' 验证该值是正实数。
如果 Double .TryParse(txtPhysicalRehabCharges.Text,dblPhysicalRehabCharges)
dblPhysicalRehabCharges> = 0 0 然后

' 调用CalcStayCharges函数并存储结果。
dblStayCharges = CalcStayCharges(dblLengthOfStay,dblCHARGE_PER_DAY)

' 致电CalcMiscCharges函数并存储结果。
dblMiscCharges = CalcMiscCharges(dblMedicationCharges,dblSurgicalCharges,
dblLabFees,dblPhysicalRehabCh arges)

' 调用CalcTotalCharges函数并存储结果。
dblTotalCharges = CalcTotalCharges(dblStayCharges,dblMiscCharges)

' 显示总费用作为输出到用户。
lblTotalCharges.Text = dblTotalCharges.ToString( c
其他
' 显示向用户发送的消息,指示物理
康复费用的值无效。
我ssageBox.Show( 身体康复费必须&
是一个正实数。 输入无效
结束 如果
否则
' 向用户显示一条消息,指示实验室费用的值无效。
MessageBox.Show( 实验室费用必须是正实数。 输入无效
结束 如果
否则
' 向用户显示消息,指示手术费用的值无效。
MessageBox.Show( 手术费用必须是正实数。 无效输入
结束 如果
其他
' 向用户显示一条消息,表明药费的价值无效。
MessageBox.Show( 药物费用必须是正实数。 输入无效
结束 如果
其他
' 向用户显示一条消息,指示停留时间的值无效。
MessageBox.Show( 逗留时长必须是正实数。 输入无效
结束 如果
结束 Sub

解决方案

我不会嵌套条件,我不会只是<$ c $中的错误代码c>否则条件:我将反转条件并报告错误为 If 部分。

Dim dblLengthOfStay As Double
< span class =code-keyword>如果 Double .TryParse(txtLengthOfStay.Text ,dblLengthOfStay) OrElse dblLengthOfStay< 0 0 然后
MessageBox.Show( 逗留时间必须是正实数。 输入无效
返回
结束 如果

并重复这些块,直到检查完所有值。


' Local variable declarations
        Dim dblLengthOfStay As Double           ' To hold the number of days in the hospital
        Dim dblMedicationCharges As Double      ' To hold the medication charges
        Dim dblSurgicalCharges As Double        ' To hold the surgical charges
        Dim dblLabFees As Double                ' To hold the lab fees
        Dim dblPhysicalRehabCharges As Double   ' To hold the physical rehabilitation charges
        Dim dblStayCharges As Double            ' To hold the base charges for the hospital stay
        Dim dblMiscCharges As Double            ' To hold the total of miscelaneous charges
        Dim dblTotalCharges As Double           ' To hold the total charges

        ' Get the length of stay as input from the user.
        ' Verify that the value is a positive real number.
        If Double.TryParse(txtLengthOfStay.Text, dblLengthOfStay) And
            dblLengthOfStay >= 0.0 Then

            ' Get the medication charges as input from the user.
            ' Verify that the value is a positive real number.
            If Double.TryParse(txtMedicationCharges.Text, dblMedicationCharges) And
                dblMedicationCharges >= 0.0 Then

                ' Get the surgical charges as input from the user.
                ' Verify that the value is a positive real number.
                If Double.TryParse(txtSurgicalCharges.Text, dblSurgicalCharges) And
                    dblSurgicalCharges >= 0.0 Then

                    ' Get the lab fees as input from the user.
                    ' Verify that the value is a positive real number.
                    If Double.TryParse(txtLabFees.Text, dblLabFees) And
                        dblLabFees >= 0.0 Then

                        ' Get the physical rehabilitation charges as input from the user.
                        ' Verify that the value is a positive real number.
                        If Double.TryParse(txtPhysicalRehabCharges.Text, dblPhysicalRehabCharges) And
                            dblPhysicalRehabCharges >= 0.0 Then

                            ' Call the CalcStayCharges function and store the result.
                            dblStayCharges = CalcStayCharges(dblLengthOfStay, dblCHARGE_PER_DAY)

                            ' Call the CalcMiscCharges function and store the result.
                            dblMiscCharges = CalcMiscCharges(dblMedicationCharges, dblSurgicalCharges,
                                                             dblLabFees, dblPhysicalRehabCharges)

                            ' Call the CalcTotalCharges function and store the result.
                            dblTotalCharges = CalcTotalCharges(dblStayCharges, dblMiscCharges)

                            ' Display the total charges as output to the user.
                            lblTotalCharges.Text = dblTotalCharges.ToString("c")
                        Else
                            ' Display a message to the user indicating that the value for the physical
                            ' rehabilitation charges is invalid.
                            MessageBox.Show("The physical rehabilitation charges must " &
                                            "be a positive real number.", "Invalid Input")
                        End If
                    Else
                        ' Display a message to the user indicating that the value for the lab fees is invalid.
                        MessageBox.Show("The lab fees must be a positive real number.", "Invalid Input")
                    End If
                Else
                    ' Display a message to the user indicating that the value for the surgical charges is invalid.
                    MessageBox.Show("The surgical charges must be a positive real number.", "Invalid Input")
                End If
            Else
                ' Display a message to the user indicating that the value for the medication charges is invalid.
                MessageBox.Show("The medication charges must be a positive real number.", "Invalid Input")
            End If
        Else
            ' Display a message to the user indicating that the value for the length of stay is invalid.
            MessageBox.Show("The length of stay must be a positive real number.", "Invalid Input")
        End If
    End Sub

解决方案

I wouldn't nest the conditions, and I wouldn't but the error code in the Else condition: I'd reverse the conditions and report the error as the If part.

Dim dblLengthOfStay As Double
If Not Double.TryParse(txtLengthOfStay.Text, dblLengthOfStay) OrElse dblLengthOfStay < 0.0 Then
   MessageBox.Show("The length of stay must be a positive real number.", "Invalid Input")
   Return
End If

And repeat the blocks until all values are checked.


这篇关于编写此代码的另一种方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆