2014-03-31 59 views
1

对不起,如果这是一个真的基本问题,但我的教科书说修改btnGet_Click程序使用For Each ... Next语句而不是For ... Next声明。我不能让它正常工作,所以这里是原来的For Each ... Next循环。为每个下一个循环为下一个循环最高编号

Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click 
    ' displays the highest commission and the 
    ' number who were paid that amount 

    Dim intCommissions() As Integer = {2500, 3400, 1000, 
             3400, 2500, 1000, 
             2850, 3000, 2780, 1890} 
    Dim intLastSub As Integer = 
     intCommissions.GetUpperBound(0) 
    Dim intHighest As Integer = intCommissions(0) 
    Dim intSalesPeople As Integer = 1 

    For intSub As Integer = 1 To intLastSub 
     If intCommissions(intSub) = intHighest Then 
      intSalesPeople += 1 
     Else 
      If intCommissions(intSub) > intHighest Then 
       intHighest = intCommissions(intSub) 
       intSalesPeople = 1 
      End If 
     End If 
    Next intSub 

    lblHighest.Text = intHighest.ToString("C0") 
    lblSalespeople.Text = intSalesPeople.ToString 
End Sub 
+0

编辑您的文章也告诉我们你用试了一下'对于每个..',告诉我们什么是没有与那工作。 – RBarryYoung

回答

3

您只需要在intCommissions阵列运行的每个:

Dim intCommissions() As Integer = {2500, 3400, 1000, 
           3400, 2500, 1000, 
           2850, 3000, 2780, 1890} 
    Dim intHighest As Integer = intCommissions(0) 
    Dim intSalesPeople As Integer = 1 

    For Each intCommission In intCommissions 
     If intCommission = intHighest Then 
      intSalesPeople += 1 
     Else 
      If intCommission > intHighest Then 
       intHighest = intCommission 
       intSalesPeople = 1 
      End If 
     End If 
    Next 
1

不是你的书需要你做,所以这不是严格意义上的回答你的问题,但如果你想给看看LINQ的.....

Dim intHighest = intCommissions.Max() 
Dim intSalesPeople = intCommissions.Where(Function(x) x = intHighest).Count() 

lblHighest.Text = intHighest.ToString("C0") 
lblSalespeople.Text intSalesPeople.ToString) 
1
Dim intCommissions() As Integer = {2500, 3400, 1000, 
             3400, 2500, 1000, 
             2850, 3000, 2780, 1890} 
    Dim intLastSub As Integer = 
     intCommissions.GetUpperBound(0) 
    Dim intHighest As Integer = intCommissions(0) 
    Dim intSalesPeople As Integer = 0 ' you need set 0. 

    For Each intCurrent In intCommissions 
     If intCurrent = intHighest Then 
      intSalesPeople += 1 
     Else 
      If intCurrent > intHighest Then 
       intHighest = intCurrent 
       intSalesPeople = 1 
      End If 
     End If 

    Next 

    Label1.Text = intHighest.ToString("C0") 
    TextBox1.Text = intSalesPeople.ToString 
相关问题