2015-11-17 73 views
0

我有VB程序,用戶是使用InputBox輸入成績。無論用戶輸入什麼,都會顯示一個消息框(msgbox),指出「請輸入一個數字」。如果沒有輸入號碼,我該如何改變它才顯示這條信息?VB迷惑了,如果路徑

Option Strict Off 

Public Class Form1 

Dim totalpointsaccumultator As Object 

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

Public Sub assignButton_Click(sender As Object, e As EventArgs) Handles assignButton.Click 
    Dim inputProjectPoints, inputTestPoints As String 
    Dim grade, projectpoints, testpoints As String 
    Dim projectcounter As Integer = 1 
    Dim testcounter As Integer = 1 
    Dim isconverted As Boolean 
    Dim totalpointsaccumulator As Integer 
    Do While projectcounter < 5 
     inputProjectPoints = InputBox("Enter the points earned on project " & projectcounter, "Grade Calculator", "0") 
     inputProjectPoints = projectpoints 
     isconverted = Integer.TryParse(inputProjectPoints, CInt(projectpoints)) 
     If isconverted Then 
      totalpointsaccumultator = totalpointsaccumulator + projectpoints 
      projectcounter = projectcounter + 1 
     Else 
      MessageBox.Show("Please enter a number.", "Grade Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information) 
     End If 
    Loop 

    Do While testcounter < 3 
     inputTestPoints = InputBox("Enter the points earned on test " & testcounter, "Grade Calculator", "0") 
     isconverted = Integer.TryParse(inputTestPoints, testpoints) 
     If isconverted Then 
      testcounter = testcounter + 1 
      totalpointsaccumulator = CInt(totalpointsaccumulator + testpoints) 
     Else 
      MessageBox.Show("Please enter a number.", "Grade calculator", MessageBoxButtons.OK, MessageBoxIcon.Information) 
     End If 
    Loop 

    ' assign grade 
    Select Case totalpointsaccumulator 
     Case Is >= 360 
      grade = "A" 
     Case Is >= 320 
      grade = "B" 
     Case Is >= 280 
      grade = "C" 
     Case Is >= 240 
      grade = "D" 
     Case Else 
      grade = "F" 
    End Select 
    totalpointsLabel.Text = Convert.ToString(totalpointsaccumulator) 
    gradeLabel.Text = grade 
End Sub 
End Class 
+0

我會簡化代碼,然後在這裏呈現仍然產生錯誤的最小可能版本。這不是最小的可能版本。機會很高,你會發現自己的錯誤或來自這裏的人會發現它更快。 – Trilarion

+1

看起來像你在這裏覆蓋你的輸入:「inputProjectPoints = projectpoints」 –

+3

Turn Option Strict ON not off! – Plutonix

回答

1
isconverted = Integer.TryParse(inputProjectPoints, CInt(projectpoints)) 

應該是:

isconverted = Integer.TryParse(inputProjectPoints, projectpoints) 

還有:

Dim grade, projectpoints, testpoints As String 

應該是:

Dim grade as String 
Dim projectpoints, testpoints As Integer 

你不能PA通過嘗試轉換/轉換它來引用一個引用變量作爲不同類型,它只是返回所需類型的值(如果可能的話,考慮到您使用Integer.TryParse(),這實際上並不具有諷刺意味)更改您聲明的變量的基礎類型。

由於這個問題,你的Integer.TryParse()總是失敗,意思是轉換總是爲false,你總是會得到消息框。

編輯:忘了補充,Plutonix是對的。設置選項嚴格開啓!