将我的代码放入嵌套循环中

时间:2015-10-19 08:12:28

标签: vb.net nested-loops

任何人都可以帮我把这段代码放到嵌套循环中。这是一个代码,用于计算按优先顺序排列的候选人的最低分数,其中1为首选,5为最高。

Dim min = 0
    If Candidate1total > min Then
    min = Candidate1total
    LabelWINNER.Text = EnterNames.Cand1Name.Text
End If
If Candidate2total < min Then
    min = Candidate2total
    LabelWINNER.Text = EnterNames.Cand2Name.Text
End If
If Candidate3total < min Then
    min = Candidate3total
    LabelWINNER.Text = EnterNames.Cand3Name.Text
End If
If Candidate4total < min Then
    min = Candidate4total
    LabelWINNER.Text = EnterNames.Cand4Name.Text
End If
If candidate5total < min Then
    min = candidate5total
    LabelWINNER.Text = EnterNames.Cand5Name.Text
End If

Candidate1Total to Candidate5Total是候选人的总分数。这是通过计算用户投票的次数来计算的。然后,如果该候选人低于minuim,那么候选人在ENTERNAMES表格上给出的名称将被放置在labelWINNER中。

我认为候选人1总数为0,我不是100%肯定,因此请求帮助。

1 个答案:

答案 0 :(得分:0)

这就是我要做的事情:

Dim Candidates = _
{ _
    New With { .Candidate = EnterNames.Cand1Name.Text, .Total = Candidate1total }, _
    New With { .Candidate = EnterNames.Cand2Name.Text, .Total = Candidate2total }, _
    New With { .Candidate = EnterNames.Cand3Name.Text, .Total = Candidate3total }, _
    New With { .Candidate = EnterNames.Cand4Name.Text, .Total = Candidate4total }, _
    New With { .Candidate = EnterNames.Cand5Name.Text, .Total = Candidate5total } _
}

Dim result = _
    Candidates _
        .OrderBy(Function (c) c.Total) _
        .GroupBy(Function (c) c.Total, Function (c) c.Candidate) _
        .Select(Function (c) New With { .Candidate = String.Join(", ", c), .Total = c.Key }) _
        .First()

所以,为了看到结果,我使用了这个示例数据:

Dim Candidates = _
{ _
    New With { .Candidate = "Mike", .Total = 5 }, _
    New With { .Candidate = "Tony", .Total = 2 }, _
    New With { .Candidate = "Bill", .Total = 2 }, _
    New With { .Candidate = "Carl", .Total = 3 }, _
    New With { .Candidate = "Dick", .Total = 4 } _
}

我得到了这个结果:

result

当然,如果它只是一个总分最低的候选人,那么Candidate字段中只会有一个名称。