2014-01-22 70 views
0

所以這個程序的任務是爲汽車租賃公司創建一個程序。不同種類的汽車是通過單選按鈕選擇的,汽車租賃的成本(每天)乘以出租的天數。我已經完成了所有的書,所以怎麼了?它不斷想出的消息框我編寫告訴我,我在輸入數據不是數字(這是)爲什麼我在VB中的代碼不工作?

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 
+0

請準確描述您的代碼出了什麼問題。我們不會做猜測工作,這是承包商通常會做的,而且他們收取很大的費用。 – Neolisk

+3

'txtDays'的聲明不包含在您的文章中。它是什麼?它實際上是**文本**。等一下!不,因爲你調用了'.Focus()'方法,而且一個字符串沒有* .Focus()*事件。你實際上應該**閱讀代碼**。 (調試器也會告訴你這一點;你應該學會現在使用它。) –

+0

txtDays是一個文本框,如果通過命名約定 – Zeddy

回答

4

我沒有足夠的信譽分置評,所以我不得不添加此作爲答案,但看起來您正在使用txtDays而不是txtDays.Text開始。

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 
0

就像Macoms01提到你需要使用txtDays.Text來檢索文本框中輸入值。此外,在計算總成本之前,您需要將decCost變量初始化爲0,以便以OR開始,編寫IF語句以檢查decCost = null,如果情況如此,則設置爲0。

最後,不是每個人都希望得到他們的建議付款,所以不要聽Neolisk。我遇到過像他這樣的人,他們只是想提供一個簡單的答案。他們忘記明白,如果有一個簡單的答案,人們就不會在這裏尋求幫助。

無論如何,如果我的回覆對你有幫助,請在這裏回答其他人的問題。

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 

一些錯誤或錯別字馬上跳出來,上面的代碼應該正常工作,請嘗試一下,看看。

相關問題