2012-03-26 411 views
0

由于我不断收到关于该错误信息的错误消息,所以我对平均输出进行了评论。 我出来口口声声说:阵列计算最小值,最大值和平均值输出:最小值,最大值和平均值

最大值:33
最小值:33

我在做什么错?

Option Explicit On 
Option Strict On 
Module MinMax 
Sub Main() 
    ' Declare a named constant for array size here 
    Const MAX_NUMS As Integer = 10 

    ' Declare array here 
    Dim numbers() As Integer = {33, 12, -6, 1001, 57, -1, 999, 365, 921, 724} 
    'Dim num1 As Integer 
    'Dim num2 As Integer 
    ' Use this integer variable as your loop index 
    Dim loopIndex As Integer = 0 

    ' Use this variable to store the number input by user 
    Dim value As Integer 

    ' String version of number input by user 
    Dim valueString As String 

    ' Use these variables to store the minimim and maximum values 
    Dim min As Integer 
    Dim max As Integer 

    ' Use these variables to store the total and the average 
    Dim total As Double 
    Dim average As Double 

    ' Write a loop to get values from user and assign to array 
    For loopIndex = 0 To MAX_NUMS - 1 
     valueString = InputBox$("Enter a value: ") 
     value = Convert.ToInt32(valueString) 
     ' Assign value to array 
     'num1 = loopIndex + 1 
     'num2 = loopIndex - 1 
    Next loopIndex 
    ' Assign the first element in the array to be the minimum and the maximum 
    min = numbers(0) 
    max = numbers(0) 
    ' Start out your total with the value of the first element in the array 
    total = numbers(0) 
    ' Write a loop here to access array values starting with numbers(1) 
    'For loopIndex = 0 To MAX_NUMS - 1 
    ' loopIndex(0) + MAX_NUMS = max 
    ' loopIndex(0) - MAX_NUMS = min 
    ' Next loopIndex 
    ' Within the loop test for minimum and maximum values 

    ' Also accumulate a total of all values 


    ' Calculate the average of the 10 values 
    average = loopIndex/MAX_NUMS 

    ' Print the values stored in the numbers array 

    ' Print the maximum value, minimum value, and average 
    System.Console.WriteLine("Maximum value: " & max) 
    System.Console.WriteLine("Minimum value: " & min) 
    ' System.Comsole.WriteLine("Average = " & average) 
End Sub 
End Module 
+1

可能是因为'min = numbers(0)'和'max = numbers(0)'都指向数组中的第一个项目,它是33 ...除非您认为注释实际上会按照他们的意思去做。 – assylias 2012-03-26 15:24:15

+1

您将'min'分配给'numbers(0)'和'max'以'numbers(0)'。这意味着您将最小值和最大值分配给阵列的第一个数字。你对结果感到惊讶吗? – 2012-03-26 15:26:32

+0

我想平均也总是出来到0.9。 – APrough 2012-03-26 15:30:47

回答

2

删除,不影响输出的代码,你留下了这一点:

Option Explicit On 
Option Strict On 

Module MinMax 
    Sub Main() 
     Dim numbers() As Integer = {33, 12, -6, 1001, 57, -1, 999, 365, 921, 724} 

     Dim min As Integer 
     Dim max As Integer 

     min = numbers(0) 
     max = numbers(0) 

     System.Console.WriteLine("Maximum value: " & max) 
     System.Console.WriteLine("Minimum value: " & min) 
    End Sub 
End Module 

基本上,你:

  1. 声明整数数组。
  2. 声明两个默认整数。
  3. 将两个默认整数的值分配给数组的第一个整数(33)。
  4. 将两个整数写入控制台。

在您所有的其他代码中,您不需要修改数组或输出的两个整数。

2

你的代码很奇怪。你有一个常数初始化数组,但你似乎想用户输入数组值,如果是那样的话,你应该修改第一个循环是这样

For loopIndex = 0 To MAX_NUMS - 1 
     valueString = InputBox$("Enter a value: ") 
     value = Convert.ToInt32(valueString) 
     ' Assign value to array 
     numbers(loopIndex) = value 
    Next loopIndex 

接下来你想获得最小,最大和平均数,这部分代码是好的:

min = numbers(0) 
max = numbers(0) 
' Start out your total with the value of the first element in the array 
total = numbers(0) 

然后你已经评论了应该做实际工作的循环。你可以这样写它

' We start loop with index 1 since index 0 is already accounted for 

    For loopIndex = 1 To MAX_NUMS - 1 
     If numbers(loopIndex) < min Then min = numbers(loopIndex) 
     If numbers(loopIndex) > max Then max = numbers(loopIndex) 
     ' accrue total value 
     total = total + numbers(loopIndex) 
    Next loopIndex 



    ' After the loop ends, the minimum value and the maximum value will be correctly 
    ' placed in min and max respectively 

    ' Now compute average 
    average = total/MAX_NUMs 

希望这已经够清楚了。祝你好运!

相关问题