【问题标题】:for each next loop to for next loop highest number对于每个下一个循环到下一个循环最大数
【发布时间】:2014-04-01 01:23:21
【问题描述】:

对不起,如果这是一个真的基本问题,但我的教科书说要修改 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 

【问题讨论】:

  • 编辑您的帖子,向我们展示您使用For Each .. 尝试过的内容,并告诉我们哪些内容无法使用。

标签: vb.net


【解决方案1】:

您只需在 intCommissions 数组上运行 for each:

    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

【讨论】:

    【解决方案2】:

    不是你的书要求你做的,所以这不是你问题的严格答案,但如果你想看看 Linq .....

    Dim intHighest = intCommissions.Max()
    Dim intSalesPeople = intCommissions.Where(Function(x) x = intHighest ).Count()
    
    lblHighest.Text = intHighest.ToString("C0")
    lblSalespeople.Text intSalesPeople.ToString)
    

    【讨论】:

      【解决方案3】:
          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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-27
        • 2017-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多