I'm working on a score sheet program for the card game rummy. I'm having a problem displaying the name of the winner in a message box. The winner is the player with the fewest points. I put the final scores in an array and I am able to get the smallest score, but can't figure out how to display the wining player's name in a message box based on the score. Right now the message box just displays the first players name. The players names are displayed in labels in a different button. Thanks guys.
Here's the code:
Private Sub btnAddScores_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddScores.Click
'player 1 scores:
Dim Player1Game1 As Integer
Dim Player1Game2 As Integer
Dim Player1Game3 As Integer
Dim Player1Game4 As Integer
Dim Player1Game5 As Integer
Dim Player1Game6 As Integer
Dim Player1Game7 As Integer
Dim Player1Game8 As Integer
Dim Player1Game9 As Integer
Dim Player1Game10 As Integer
Dim Player1FinalScore As Integer
'player 2 scores:
Dim Player2Game1 As Integer
Dim Player2Game2 As Integer
Dim Player2Game3 As Integer
Dim Player2Game4 As Integer
Dim Player2Game5 As Integer
Dim Player2Game6 As Integer
Dim Player2Game7 As Integer
Dim Player2Game8 As Integer
Dim Player2Game9 As Integer
Dim Player2Game10 As Integer
Dim Player2FinalScore As Integer
'player 3 scores:
Dim Player3Game1 As Integer
Dim Player3Game2 As Integer
Dim Player3Game3 As Integer
Dim Player3Game4 As Integer
Dim Player3Game5 As Integer
Dim Player3Game6 As Integer
Dim Player3Game7 As Integer
Dim Player3Game8 As Integer
Dim Player3Game9 As Integer
Dim Player3Game10 As Integer
Dim Player3FinalScore As Integer
'player 4 scores:
Dim Player4Game1 As Integer
Dim Player4Game2 As Integer
Dim Player4Game3 As Integer
Dim Player4Game4 As Integer
Dim Player4Game5 As Integer
Dim Player4Game6 As Integer
Dim Player4Game7 As Integer
Dim Player4Game8 As Integer
Dim Player4Game9 As Integer
Dim Player4Game10 As Integer
Dim Player4FinalScore As Integer
'player 5 scores:
Dim Player5Game1 As Integer
Dim Player5Game2 As Integer
Dim Player5Game3 As Integer
Dim Player5Game4 As Integer
Dim Player5Game5 As Integer
Dim Player5Game6 As Integer
Dim Player5Game7 As Integer
Dim Player5Game8 As Integer
Dim Player5Game9 As Integer
Dim Player5Game10 As Integer
Dim Player5FinalScore As Integer
'storing player 1 scores in varables
Player1Game1 = tbPlayer1ScoreGame1.Text
Player1Game2 = tbPlayer1ScoreGame2.Text
Player1Game3 = tbPlayer1ScoreGame3.Text
Player1Game4 = tbPlayer1ScoreGame4.Text
Player1Game5 = tbPlayer1ScoreGame5.Text
Player1Game6 = tbPlayer1ScoreGame6.Text
Player1Game7 = tbPlayer1ScoreGame7.Text
Player1Game8 = tbPlayer1ScoreGame8.Text
Player1Game9 = tbPlayer1ScoreGame9.Text
Player1Game10 = tbPlayer1ScoreGame10.Text
Player1FinalScore = (Player1Game1 + Player1Game2 + Player1Game3 + Player1Game4 + Player1Game5 + Player1Game6 + Player1Game7 + Player1Game8 + Player1Game9 + Player1Game10)
'storing player 2 scores in varables
Player2Game1 = tbPlayer2ScoreGame1.Text
Player2Game2 = tbPlayer2ScoreGame2.Text
Player2Game3 = tbPlayer2ScoreGame3.Text
Player2Game4 = tbPlayer2ScoreGame4.Text
Player2Game5 = tbPlayer2ScoreGame5.Text
Player2Game6 = tbPlayer2ScoreGame6.Text
Player2Game7 = tbPlayer2ScoreGame7.Text
Player2Game8 = tbPlayer2ScoreGame8.Text
Player2Game9 = tbPlayer2ScoreGame9.Text
Player2Game10 = tbPlayer2ScoreGame10.Text
Player2FinalScore = (Player2Game1 + Player2Game2 + Player2Game3 + Player2Game4 + Player2Game5 + Player2Game6 + Player2Game7 + Player2Game8 + Player2Game9 + Player2Game10)
'storing player 3 scores in varables
Player3Game1 = tbPlayer3ScoreGame1.Text
Player3Game2 = tbPlayer3ScoreGame2.Text
Player3Game3 = tbPlayer3ScoreGame3.Text
Player3Game4 = tbPlayer3ScoreGame4.Text
Player3Game5 = tbPlayer3ScoreGame5.Text
Player3Game6 = tbPlayer3ScoreGame6.Text
Player3Game7 = tbPlayer3ScoreGame7.Text
Player3Game8 = tbPlayer3ScoreGame8.Text
Player3Game9 = tbPlayer3ScoreGame9.Text
Player3Game10 = tbPlayer3ScoreGame10.Text
Player3FinalScore = (Player3Game1 + Player3Game2 + Player3Game3 + Player3Game4 + Player3Game5 + Player3Game6 + Player3Game7 + Player3Game8 + Player3Game9 + Player3Game10)
'storing player 4 scores in varables
Player4Game1 = tbPlayer4ScoreGame1.Text
Player4Game2 = tbPlayer4ScoreGame2.Text
Player4Game3 = tbPlayer4ScoreGame3.Text
Player4Game4 = tbPlayer4ScoreGame4.Text
Player4Game5 = tbPlayer4ScoreGame5.Text
Player4Game6 = tbPlayer4ScoreGame6.Text
Player4Game7 = tbPlayer4ScoreGame7.Text
Player4Game8 = tbPlayer4ScoreGame8.Text
Player4Game9 = tbPlayer4ScoreGame9.Text
Player4Game10 = tbPlayer4ScoreGame10.Text
Player4FinalScore = (Player4Game1 + Player4Game2 + Player4Game3 + Player4Game4 + Player4Game5 + Player4Game6 + Player4Game7 + Player4Game8 + Player4Game9 + Player4Game10)
'storing player 5 scores in varables
Player5Game1 = tbPlayer5ScoreGame1.Text
Player5Game2 = tbPlayer5ScoreGame2.Text
Player5Game3 = tbPlayer5ScoreGame3.Text
Player5Game4 = tbPlayer5ScoreGame4.Text
Player5Game5 = tbPlayer5ScoreGame5.Text
Player5Game6 = tbPlayer5ScoreGame6.Text
Player5Game7 = tbPlayer5ScoreGame7.Text
Player5Game8 = tbPlayer5ScoreGame8.Text
Player5Game9 = tbPlayer5ScoreGame9.Text
Player5Game10 = tbPlayer5ScoreGame10.Text
Player5FinalScore = (Player5Game1 + Player5Game2 + Player5Game3 + Player5Game4 + Player5Game5 + Player5Game6 + Player5Game7 + Player5Game8 + Player5Game9 + Player5Game10)
'displays finals in labels
lblPlayer1FinalScore.Text = Player1FinalScore
lblPlayer2FinalScore.Text = Player2FinalScore
lblPlayer3FinalScore.Text = Player3FinalScore
lblPlayer4FinalScore.Text = Player4FinalScore
lblPlayer5FinalScore.Text = Player5FinalScore
Dim WonArray(4) As Integer
WonArray(0) = Player1FinalScore
WonArray(1) = Player2FinalScore
WonArray(2) = Player3FinalScore
WonArray(3) = Player4FinalScore
WonArray(4) = Player5FinalScore
Dim WiningValue As Integer = (WonArray.Min)
MsgBox(lblPlayerName1.Text)
End sub
答案 0 :(得分:0)
创建一个代表玩家数据的类
Public Class Player
Public ReadOnly Property Name As String
Public ReadOnly Property Score As Integer
Public Sub New(name As String)
Name = name
End Sub
'Function For adding scores
Public Sub AddPoints(points As Integer)
_Score += points
End Sub
End Class
然后你可以创建玩家列表,在那里添加玩家和积分并用名称计算最小结果
Private Sub btnAddScores_Click(ByVal sender As Object,
ByVal e As EventArgs) Handles btnAddScores.Click
Dim players = New List(Of Player)()
'player 1 scores:
Dim player1 As New Player("Player 1")
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame1.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame2.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame3.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame4.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame5.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame6.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame7.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame8.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame9.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame10.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame1.Text))
player1.AddPoints(Integer.Parse(tbPlayer1ScoreGame1.Text))
players.Add(player1)
'player 2 scores:
Dim player2 As New Player("Player 1")
player2.AddPoints(Integer.Parse(tbPlayer2ScoreGame1.Text))
player2.AddPoints(Integer.Parse(tbPlayer2ScoreGame2.Text))
player2.AddPoints(Integer.Parse(tbPlayer2ScoreGame3.Text))
player2.AddPoints(Integer.Parse(tbPlayer2ScoreGame4.Text))
player2.AddPoints(Integer.Parse(tbPlayer2ScoreGame5.Text))
player1.AddPoints(Integer.Parse(tbPlayer2ScoreGame6.Text))
player1.AddPoints(Integer.Parse(tbPlayer2ScoreGame7.Text))
player1.AddPoints(Integer.Parse(tbPlayer2ScoreGame8.Text))
player1.AddPoints(Integer.Parse(tbPlayer2ScoreGame9.Text))
player1.AddPoints(Integer.Parse(tbPlayer2ScoreGame10.Text))
player1.AddPoints(Integer.Parse(tbPlayer2ScoreGame1.Text))
player1.AddPoints(Integer.Parse(tbPlayer2ScoreGame1.Text))
players.Add(player2)
'and so on for other players
'displays finals in labels
lblPlayer1FinalScore.Text = player1.Score.ToString()
lblPlayer2FinalScore.Text = player2.Score.ToString()
lblPlayer3FinalScore.Text = player3.Score.ToString()
lblPlayer4FinalScore.Text = player4.Score.ToString()
lblPlayer5FinalScore.Text = player5.Score.ToString()
Dim minScores As Integer = Integer.MaxValue
Dim winner As Person
For Each tempPlayer As Player In players
If (tempPlayer.Scores < minScores)
winner = tempPlayer
End If
Next
Dim message As String = $"Winner is {winner.Name} with {winner.Score} points."
MessageBox.Show(message)
End sub
在项目中或至少在代码文件的第一行中设置Option Strict On
。这将为您提供有关编译期间可能的类型转换错误的快速反馈。
另请考虑使用DataGridView
而不是多个文本框。如果游戏数量(10)不变,那么你可以使用DataGridView
10列和5行玩家。