Vb.net 如何在Visual Basic中使用函数查找可被7整除的数字?

Vb.net 如何在Visual Basic中使用函数查找可被7整除的数字?,vb.net,Vb.net,我的任务是:显示可被7整除的数字(范围10-25)。计算并显示不能被7整除的数字的平均值 所以我用VisualBasic写了这段代码。如何改进?我不知道如何以其他方式显示数字14和21 Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim counter As Byte = 0 Dim accumulator As Short

我的任务是:显示可被7整除的数字(范围10-25)。计算并显示不能被7整除的数字的平均值

所以我用VisualBasic写了这段代码。如何改进?我不知道如何以其他方式显示数字14和21

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim counter As Byte = 0
    Dim accumulator As Short = 0
    Dim average As Single
    Dim loopcounter As Byte

    For loopcounter = 10 To 25
        If loopcounter Mod 7 <> 0 Then
            accumulator += loopcounter
            counter += 1
        Else
            Label1.Text = loopcounter & ", 14"
        End If
    Next

    average = accumulator / counter
    Label2.Text = average
End Sub
End Class
公共类表单1
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
字节为0的Dim计数器
变暗蓄能器短=0
将平均值变为单个
Dim循环计数器作为字节
对于loopcounter=10到25
如果循环计数器Mod 7 0,则
累加器+=循环计数器
计数器+=1
其他的
Label1.Text=loopcounter&“14”
如果结束
下一个
平均值=累加器/计数器
Label2.Text=平均值
端接头
末级

您可以将可被7整除的数字放入一个
列表(整数)
,然后在循环结束时使用将它们组合成一个字符串

    Dim counter As Byte = 0
    Dim accumulator As Short = 0
    Dim average As Single
    Dim loopcounter As Byte
    Dim multiples As List(Of Integer) = New List(Of Integer)()  ' Declare list

    For loopcounter = 10 To 25
        If loopcounter Mod 7 <> 0 Then
            accumulator += loopcounter
            counter += 1
        Else
            multiples.Add(loopcounter)  ' Add multiple of 7 to list
        End If
    Next

    Label1.Text = String.Join(", ", multiples) ' Combine list into a comma-separated string
    average = accumulator / counter
    Label2.Text = average
Dim计数器,字节=0
变暗蓄能器短=0
将平均值变为单个
Dim循环计数器作为字节
Dim乘为列表(整数的)=新列表(整数的)('声明列表
对于loopcounter=10到25
如果循环计数器Mod 7 0,则
累加器+=循环计数器
计数器+=1
其他的
倍数。添加(循环计数器)'将7的倍数添加到列表中
如果结束
下一个
Label1.Text=String.Join(“,”,multiples)”将列表合并为逗号分隔的字符串
平均值=累加器/计数器
Label2.Text=平均值