在降低最低值后找到数组的平均值? (VB)

时间:2017-05-24 19:39:38

标签: vb.net

我正在开发一个程序,要求用户输入5个数字。 5个数字中最低的一个将被删除,然后其他4个数字将被平均。

我对VB很陌生,但我相信我现在正走在正确的道路上......

我已经对数组进行了排序以帮助确定最低数字,但我不知道如何排除最低数字,然后平均其他剩余数字。

到目前为止,这是我的代码:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim IntArr(4) As Integer
        Dim i As Integer
        For i = 1 To 5
            IntArr(4) = InputBox("Enter Number" & i)
        Next
        Array.Sort(IntArr)
        'textbox1.text = ???
    End Sub
End Class

有人可以帮助或至少指出我正确的方向吗?

3 个答案:

答案 0 :(得分:4)

为了与代码的精神保持一致,以下内容将起作用。

Public Class Form1
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim IntArr(4) As Integer
      Dim OutArr(3) As Integer

      For i = 0 To 4
         IntArr(i) = InputBox("Enter Number " & i)
      Next

      Array.Sort(IntArr)
      Array.Copy(IntArr, 1, OutArr, 0, 4)  'exclude the lowest number
      TextBox1.Text = OutArr.Average()
   End Sub

End Class

答案 1 :(得分:1)

使用内置LINQ函数

#include <stdio.h>

#define N 10

void show_lights_all(char *label, int light[N]);
void show_lights_off(char *label, int light[N]);
void firstwalk(int light[N]);
void secondtwalk(int light[N]);
void thirdtwalk(int light[N]);
void fourthtwalk(int light[N]);

int main() {
    int light[N] = {0};
    show_lights_all("Initial state of lights-  ", light);
    firstwalk(light);
    secondtwalk(light);
    thirdtwalk(light);
    fourthtwalk(light);
    show_lights_off("\nLights that were off are :-  ", light);
}

void show_lights_all(char *label, int light[N]) {
    printf(label);
    for (int i = 0; i < N; i++) {
        printf("%d ", light[i]);
    }
}

void show_lights_off(char *label, int light[N]) {
    printf(label);
    for (int i = 0; i < N; i++) {
        if (light[i] == 0) printf("%d ", i + 1);
    }
}

void firstwalk(int light[N]) {
    show_lights_all("\nLights before the first walk-  ", light);
    for (int i = 0; i < N; i++) {
        light[i] = 0;
    }
    show_lights_all("Lights after the first walk-  ", light);
}

void secondtwalk(int light[N]) {
    show_lights_all("\nLights before the second walk-  ", light);
    for (int i = 1; i < N; i += 2) {
        light[i] = 1;
    }
    show_lights_all("Lights after the second walk-  ", light);
}

void thirdtwalk(int light[N]) {
    show_lights_all("\nLights before the third walk-  ", light);
    for (int i = 2; i < N; i += 3) {
        light[i] = light[i] == 1 ? 0 : 1;
    }
    show_lights_all("Lights after the third walk-  ", light);
}

void fourthtwalk(int light[N]) {
    show_lights_all("\nLights before the fourth walk-  ", light);
    for (int i = 3; i < N; i += 4) {
        light[i] = light[i] == 1 ? 0 : 1;
    }
    show_lights_all("Lights after the fourth walk-  ", light);
}

enter image description here

答案 2 :(得分:0)

如果我没有弄错的话,你上面的代码将继续简单地用每个框改变索引4的值。我会做这样的事情(为方便起见,我会使用你的变量名。)

Button1_Click(procedure junk that is auto-inserted)

Dim intArr(4) As Integer
Dim OutArr(3) As Integer
Dim intCounter, intAverage, intLowest, intLowIndex As Integer

'populate all indexes of intArr()
For intCounter = 0 to 4
    intArr(intCounter) = InputBox("Please enter a number.")
Next intCounter

intCounter = 1                  'reset counter for check
intLowest = intArr(intLowIndex) 'start with index 0

'find lowest number and its index
For intCounter = 1 to 4
   If intLowest > intArr(intCounter) Then     'defaults to previous
       intLowest = intArr(intCounter)
       intLowIndex = intCounter
   End If
Next intCounter

intCounter = 0   'reset counter again for possible For...Next loops

Select Case intLowIndex
Case = 0
   For intCounter = 0 to 3
      OutArr(intCounter) = intArr(intCounter + 1)
   Next intCounter
Case = 1
  OutArr(0) = intArr(0)
  OutArr(1) = intArr(2)
  OutArr(2) = intArr(3)
  OutArr(3) = intArr(4)
Case = 2
  OutArr(0) = intArr(0)
  OutArr(1) = intArr(1)
  OutArr(2) = intArr(3)
  OutArr(3) = intArr(4)
Case = 3
  OutArr(0) = intArr(0)
  OutArr(1) = intArr(1)
  OutArr(2) = intArr(2)
  OutArr(3) = intArr(4)
Case = 4
  For intCounter = 0 to 3
    OutArr(intCounter) = intArr(intCounter)
  Next intCounter
End Select

intAverage = (OutArr(0) + OutArr(1) + OutArr(2) + OutArr(3)) / 4

'insert your preferred method to display OutArr() and intAverage