2016-12-15 9 views
-2

私はクイズを作成しています。前にも述べたように、ハイスコア機能が必要です。私がプログラムを走らせると、最高得点が必ずしも最高得点ではないという点を除いて、すべてが問題ありません。クイズを作るビジュアルベーシックでは、正常に動作しないハイスコア機能が必要です。

私がそれをやり直すと、低いスコアが引き続き最高のスコアに置き換えられます。 2番目と3番目に高い数字が表示されません。

ここにコードがあります。最後の質問の終わり。)

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

を使用し、最後にあなたのスコアを割り当てるために、それはちょうどあなたが

AddScoreIfHighEnough(score,3) 

highScores() 

を使用するたびに置き換える追加使用するには ' highScore'、 'score2'と' score3'はLabelsまたはTextboxesです - ar彼らは正しい順序で配置されていますか?私が何かを逃していない限り、あなたが提供したコードはうまくいくはずです。 –

答えて

1

あなたが代わりに以下のサブを使用して、現在HighScoresサブを使用しての、少しその後、この

Dim HighScores As New List(Of Integer) 

のような整数のリストを使用して、コードを簡素化することができ、リストにスコアを追加するだけです最初に最も高い値にソートされ、次に最も低い値がリストの最後から削除されます。

このように、3つ以上の高得点を持つことにした場合、うまくいくはずです。 Ifステートメントを編集せずに、コード量を増やす代わりにmaxHighscores変数を変更するだけです。次に、追加のスコアを表示するコードを更新サブメニューに追加します。

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 

はちょうど私がいることを推測

Private Sub scoreupdate() 
    highScore.Text = HighScores(0).ToString 
    score2.Text = HighScores(1).ToString 
    score3.Text = HighScores(2).ToString 
End Sub 
関連する問題