我正在制作一张评分表,以便在智力竞赛中跟踪得分。我将三支球队得分中的每一个都保存到一个阵列的元素中,我想检查以确定是否存在平局以及哪些球队被绑定。任何人都能提供关于最有效的解决方法的指导吗?
修改
我希望有比if语句更漂亮的东西(包含在下面作为答案),但我现在还没有更好的想法。老实说,我不确定我理解为什么这个被投了票。这是真的我没有包含我的代码,因为虽然我已经考虑了一段时间的问题,但我还不知道从哪里开始。事实上,我特别要求仅仅通过正确的方式提供一些指导,因为我不想让其他人为我做我的工作。尽管如此,我将展示我试图重现的逻辑。我不确定我还能做些什么。
Team Score | Rank Description | Team Tie ID
---------------------+------------------+-------------------
/ 250 \ | High Score | / 0 \
TeamScore1 = | 30 | | Low Score | TeamTie1 = | 0 |
\ 60 / | Middle Score | \ 0 /
---------------------+------------------+-------------------
/ 250 \ | High score *TIE* | / 1 \
TeamScore2 = | 30 | | Low Score | TeamTie2 = | 0 |
\ 250 / | High score *TIE* | \ 1 /
---------------------+------------------+-------------------
/ 250 \ | High Score | / 0 \
TeamScore3 = | 30 | | Low Score *TIE* | TeamTie3 = | 2 |
\ 30 / | Low Score *TIE* | \ 2 /
基本上,每当任何一支球队在测验结束时都被束缚时,这两支球队会被问到更多问题,直到他们的分数发生变化以消除平局。我希望能够通过基于TeamTie
数组生成TeamScore
数组来检测团队何时被绑定。如果实现这一点的最好方法是使用if语句,那么很好,但我只是可能有一种更简单的方法。
答案 0 :(得分:0)
三分之二:
Public Function teext(SomeArray) As Boolean
teext = False
If SomeArray(1) = SomeArray(2) Or SomeArray(2) = SomeArray(3) Or SomeArray(1) = SomeArray(3) Then
teext = True
End If
End Function
对于较大的数组,我会使用集合
答案 1 :(得分:0)
回答问题:
无论如何,如果不循环数据,就无法获得所需的信息。
您可以结合使用WorksheetFunction.Large
方法和WorksheetFunction.Match
方法来获得与每个团队相对应的排名。
要测试是否发生平局,您需要使用SumProduct
功能。请参阅本文结尾(http://goo.gl/o9po),了解如何在VBA中使用它,因为它可能有些棘手且不直观。
注意:虽然我没有想到,也没有比上述信息更多的要求将问题标记为已回答(我相信我在我的问题中已经非常清楚),更多的是怀疑有用。鉴于此,为了将来可能会有的人的利益,这里是我最终使用的代码:
Sub Test4Tie()
' Variable declaration
Dim TeamScore(1 To 3) As Integer, NumTeams As Integer
Dim TeamRank(1 To 3) As Integer, ScoreCount(1 To 3) As Integer
Dim iTT As Integer, TeamScoreSrt(1 To 3) As Integer
Dim TeamTie As Boolean, TeamScoreStr As String
'Input the score for each team
TeamScore(1) = 250
TeamScore(2) = 30
TeamScore(3) = 30
'Input the number of teams
NumTeams = 3
For iTT = 1 To NumTeams
' Sort the teams from highest to lowest score
TeamScoreSrt(iTT) = WorksheetFunction.Large(TeamScore, iTT)
' Create list of team scores to count the number of occurances of each score in
If iTT < NumTeams Then
TeamScoreStr = TeamScoreStr & CStr(TeamScore(iTT)) & ","
Else
TeamScoreStr = TeamScoreStr & CStr(TeamScore(iTT))
End If
Next iTT
For iTT = 1 To NumTeams
' Count the number of occurances of each score
ScoreCount(iTT) = Application.Evaluate("SUMPRODUCT(--({" & TeamScoreStr & "}=" & TeamScore(iTT) & "))")
' Get the rank of each score
TeamRank(iTT) = WorksheetFunction.Match(TeamScore(iTT), TeamScoreSrt, 0)
' Detect ties
If ScoreCount(iTT) > 1 Then
TeamTie = True
End If
Debug.Print "TeamScore: " & TeamScore(iTT) & ", ScoreCount: " & ScoreCount(iTT) & ", TeamRank: " & TeamRank(iTT) & ", TeamTie: " & TeamTie
Next iTT
End Sub
以下是代码运行时的输出:
输入:
TeamScore = {250, 30, 60}
输出:
TeamScore: 250, ScoreCount: 1, TeamRank: 1, TeamTie: False
TeamScore: 30, ScoreCount: 1, TeamRank: 3, TeamTie: False
TeamScore: 60, ScoreCount: 1, TeamRank: 2, TeamTie: False
输入:
TeamScore = {250, 30, 250}
输出:
TeamScore: 250, ScoreCount: 1, TeamRank: 1, TeamTie: False
TeamScore: 30, ScoreCount: 1, TeamRank: 3, TeamTie: False
TeamScore: 50, ScoreCount: 1, TeamRank: 2, TeamTie: False
输入:
TeamScore = {250, 30, 30}
输出:
TeamScore: 250, ScoreCount: 1, TeamRank: 1, TeamTie: False
TeamScore: 30, ScoreCount: 2, TeamRank: 2, TeamTie: True
TeamScore: 30, ScoreCount: 2, TeamRank: 2, TeamTie: True