进行测验在visual basic中需要高分功能才能正常工作

时间:2016-12-15 05:58:05

标签: vb.net

我正在进行测验,它需要如前所述的高分数功能。当我运行该程序时,一切都很好,除了最高分并不总是最高分。

如果我重新获得它,较低的分数仍将取代最高分。分数没有显示在我认为具有相同问题的第2和第3最高括号中。

这是代码 - (正确回答每个问题得分+ = 1,然后在最后一个问题结束时调用分数更新。)

Public score As Integer = 0
'best score
Dim highest As Integer
'second best
Dim scoreuno As Integer
'third best
Dim scoredos As Integer


Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub scoreupdate()
    highScore.Text = highest.ToString
    score2.Text = scoreuno.ToString
    score3.Text = scoredos.ToString
End Sub

Public Sub highscores()
    If score > highest Then
        scoredos = scoreuno
        scoreuno = highest
        highest = score
    ElseIf score > scoreuno Then
        scoredos = scoreuno
        scoreuno = score
    ElseIf score > scoredos Then
        scoredos = score
    End If
    scoreupdate()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    score = 0
    Form1.Show()
    Me.Close()
End Sub

1 个答案:

答案 0 :(得分:1)

您可以使用像这样的整数列表来简化您的代码

Dim HighScores As New List(Of Integer)

然后,不要使用下面的子项使用当前的HighScores子,而只需在列表中添加一个分数,该分数首先排序为最高值,然后从列表末尾删除最低值

这样,如果你决定获得超过3个高分,它应该可以正常工作。只需更改maxHighscores变量,而不必编辑If语句并添加越来越多的代码。然后只需将代码添加到更新子中以显示其他分数。

Private Sub AddScoreIfHighEnough(score As Integer, maxScoresCount As Integer)
    'Adds score to list
    HighScores.Add(score)
    'Sorts list into ascending order and reverses the order
    HighScores.Sort()
    HighScores.Reverse()
    'if the number of scores is greater than the maximum allowed
    'number remove the extra scores
    While maxScoresCount < HighScores.Count
        HighScores.RemoveAt(HighScores.Count - 1)
    End While
End Sub

要使用它,只需在每次使用时添加替换

highScores()

AddScoreIfHighEnough(score,3)

最后要分配你的分数,只需使用

Private Sub scoreupdate()
    highScore.Text = HighScores(0).ToString
    score2.Text = HighScores(1).ToString
    score3.Text = HighScores(2).ToString
End Sub