0

因此,对于这个程序,任务是为汽车租赁公司创建一个程序。不同种类的汽车由单选按钮选择,汽车租赁成本(每天)乘以租赁天数。我已经按照书本做了所有事情,所以有什么问题?它不断出现我编码的消息框,告诉我我输入的数据不是数字(它是)

    Dim decJeepWrangler As Decimal = 55D
    Dim decLandRover As Decimal = 125D
    Dim decPickup As Decimal = 85D

    Dim intDays As Integer
    Dim decTotalCost As Decimal
    Dim decCost As Decimal

    If IsNumeric(txtDays) Then
        intDays = Convert.ToInt32(txtDays)

        If intDays > 0 Then
            If radJeepWrangler.Checked Then
                decCost = decJeepWrangler
            ElseIf radLandRover.Checked Then
                decCost = decLandRover
            ElseIf radPickup.Checked Then
                decCost = decPickup
            End If
            decTotalCost = intDays * decCost
            lblTotalCost.Text = decTotalCost.ToString("C")
        Else
            MsgBox("You entered " & intDays.ToString() & ". Enter a positive number", , "Input Error")
            txtDays.Text = ""
            txtDays.Focus()
        End If
    Else
        MsgBox("Enter how many days you will be renting", , "Input Error")
        txtDays.Text = ""
        txtDays.Focus()
    End If
End Sub
4

4 回答 4

4

我没有足够的声誉点来评论,所以我不得不添加这个作为答案,但看起来你正在使用txtDays而不是txtDays.Text开始。

于 2014-01-22T02:07:26.410 回答
1

这是你必须做的...

1) Add a TextBox control and name it txtDays.
2) Add a button.
3) Add the code shown below under the button_click event.

    Dim decJeepWrangler As Decimal = 55D
    Dim decLandRover As Decimal = 125D
    Dim decPickup As Decimal = 85D

    Dim intDays As Integer
    Dim decTotalCost As Decimal
    Dim decCost As Decimal

    If IsNumeric(txtDays.Text) Then
        intDays = Convert.ToInt32(txtDays.Text)

        If intDays > 0 Then
            If radJeepWrangler.Checked Then
                decCost = decJeepWrangler
            ElseIf radLandRover.Checked Then
                decCost = decLandRover
            ElseIf radPickup.Checked Then
                decCost = decPickup
            End If
            decTotalCost = intDays * decCost
            lblTotalCost.Text = decTotalCost.ToString("C")
        Else
            MsgBox("You entered " & intDays.ToString() & ". Enter a positive number", , "Input Error")
            txtDays.Text = ""
            txtDays.Focus()
        End If
    Else
        MsgBox("Enter how many days you will be renting", , "Input Error")
        txtDays.Text = ""
        txtDays.Focus()
    End If
于 2014-01-22T02:27:24.680 回答
0

就像Macoms01提到的,您需要使用txtDays.Text从 TextBox 检索输入值。此外,在计算总成本之前,您需要将 decCost 变量初始化为 0 以 OR 开始,编写一个 IF 语句来检查 decCost = null 并在这种情况下设置为 0。

最后,并不是每个人都希望从他们的建议中获得报酬,所以不要听 Neolisk。我在这里遇到过像他这样的人,他们只是想提供一个简单的答案。他们忘记了如果有一个简单的答案,人们就不会在这里寻求帮助。

无论如何,如果我的回答对您有任何帮助,请在此处为其他人回答问题。

于 2014-01-22T02:34:48.340 回答
0
Dim decJeepWrangler As Decimal = 55D
Dim decLandRover As Decimal = 125D
Dim decPickup As Decimal = 85D
Dim intDays As Integer = 0
Dim decTotalCost As Decimal = 0
Dim decCost As Decimal = 0
'
If Trim(txtDays.text) <> "" Then
    If cint(trim(txtDays.text)) > 0 Then
        intDays = CInt(txtDays.Text)
        If radJeepWrangler.Checked Then
            decCost = decJeepWrangler
        ElseIf radLandRover.Checked Then
            decCost = decLandRover
        ElseIf radPickup.Checked Then
            decCost = decPickup
        End if
        if decCost > 0 then
            decTotalCost = intDays * decCost
            lblTotalCost.Text = decTotalCost.ToString()
        Else
            msgbox("please select a vehicle", ,"Error")
            txtDays.Text = "0"
            txtDays.Focus()
        End If
    Else
        MsgBox("Enter how many days you will be renting", , "Error")
        txtDays.Text = "0"
        txtDays.Focus()
    End If
else
    MsgBox("Enter how many days you will be renting", , "Error")
    txtDays.Text = "0"
    txtDays.Focus()
end if

一些错误或拼写错误会立即跳出来,上面的代码应该可以正常工作,请尝试一下。

于 2014-01-22T04:52:58.650 回答