我做了一個測驗,在測驗結束時,用戶會根據他們的表現獲得反饋。這是代碼:
Private Sub btnFinalScore_Click(sender As Object, e As EventArgs) Handles btnFinalScore.Click
lblScore11.Text = Val(lblScore10.Text)
If lblScore11.Text = 100 Then 'Deals if the user gets full marks
txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
ElseIf lblScore11.Text = 90 Or 80 Or 70 Then 'Deals if the user gets between 70% to 90%l marks
txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
ElseIf lblScore11.Text = 60 Or 50 Or 40 Then 'Deals if the user gets between 40% to 60%l marks
txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
ElseIf lblScore11.Text = 30 Or 20 Or 10 Then 'Deals if the user gets between 10% to 30%l marks
txtFinalFeedback.AppendText("You got a a lot of questions wrong, remember to go over these topics and repeat the quiz later")
Else
lblScore11.Text = 0 'Deals if the user gets no marks
txtFinalFeedback.AppendText("You got all the questions wrong, make sure to go over all the topics and repeat the quiz later")
End If
End Sub
如果用戶得到 100,第一行代碼作業正常,但如果用戶有任何錯誤,總是給出第二個反饋。我該如何解決?
uj5u.com熱心網友回復:
您應該將分數作為數值處理,而不是作為文本處理。
那么你只需要檢查分數是否大于或等于每個括號中的最低分數:
Private Sub btnFinalScore_Click(sender As Object, e As EventArgs) Handles btnFinalScore.Click
lblScore11.Text = lblScore10.Text
Dim score As Integer = CInt(lblScore10.Text)
If score = 100 Then 'Deals if the user gets full marks
txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
ElseIf score >= 70 Then 'Deals if the user gets between 70% to 90%l marks
txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
ElseIf score >= 40 Then 'Deals if the user gets between 40% to 60%l marks
txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
ElseIf score >= 10 Then 'Deals if the user gets between 10% to 30%l marks
txtFinalFeedback.AppendText("You got a a lot of questions wrong, remember to go over these topics and repeat the quiz later")
Else
txtFinalFeedback.AppendText("You got all the questions wrong, make sure to go over all the topics and repeat the quiz later")
End If
End Sub
uj5u.com熱心網友回復:
您應該查看您的命名約定。lblScore11、lblScore10 是可怕的名字,很容易混淆和錯誤。txtFinalFeedback 是有道理的。
并且這段代碼應該更簡單明了。為什么不直接獲取 lblScore10 的值。
對于這種型別的構造,我認為使用 select case 陳述句更優雅,如果您將使用數字范圍,這也很有用。因此,代碼可以像這樣重構:
Select Case score
Case 100 'Deals if the user gets full marks
txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
Case Is >= 70 'Deals if the user gets between 70% to 90%l marks
txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
Case Is >= 40 'Deals if the user gets between 40% to 60%l marks
txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
Case Is >= 10 'Deals if the user gets between 10% to 30%l marks
txtFinalFeedback.AppendText("You got a a lot of questions wrong, remember to go over these topics and repeat the quiz later")
Case Else
txtFinalFeedback.AppendText("You got all the questions wrong, make sure to go over all the topics and repeat the quiz later")
End Select
uj5u.com熱心網友回復:
你應該設定Option Strict On. 請參閱:Option Strict 和 Option Explicit 有什么作用?. 如果不這樣做,則在進行不需要的轉換時將不會收到錯誤訊息。
你寫ElseIf lblScore11.Text = 90 Or 80 Or 70 Then。這不符合您的預期,因為您必須撰寫ElseIf lblScore11.Text = 90 Or lblScore11.Text = 80 Or lblScore11.Text = 70 Then. 它現在所做的是將 80 和 90 轉換為True,因為那里需要一個布林值。所以,確實如此,而且ElseIf (lblScore11.Text = 90) Or True Or True Then始終如此True。或者它是否進行按位或80 Or 70并將其轉換為True以將其與第一個結果進行比較。我不確定。但無論如何都是錯誤的。
另外,如果涉及其他值(例如 54)會發生什么?你只是無視他們。
您還將字串與整數進行比較。將字串轉換為整數,然后進行比較。請參閱@Idle_Mind 的答案以獲得一個好的解決方案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/343691.html
標籤:网络
