1

提前感谢您提供的任何帮助。

下面是一个简单的编程作业,我正在尝试上交家庭作业...我在程序的第 65 行和第 69 行写了两个函数 * MARKED 前面有注释 1 行,函数在第 93 行中定义- 101 和 103-111 *还标记了前面的注释 1 行。当程序运行时,它为“最大降雨量”和“最小降雨量”返回 0 值,并且不提供月份(索引)的任何值。我的目标是显示它所在月份的最大降雨量和它发生月份的最小降雨量......我是否正确地接近这个?

感谢任何时候。

    Module Module1

Sub Main()
    'declare constant for array size- to be used in parrallel arrays 
    Const SIZE As Integer = 12

    'declare parrallel arrays rainfall and month
    Dim rainfall(SIZE) As Double
    Dim month(SIZE) As String
    month(0) = "January"
    month(1) = "February"
    month(2) = "March"
    month(3) = "April"
    month(4) = "May"
    month(5) = "June"
    month(6) = "July"
    month(7) = "August"
    month(8) = "September"
    month(9) = "October"
    month(10) = "November"
    month(11) = "December"

    'declare other variables
    Dim total As Double
    Dim average As Double
    Dim index As Integer
    Dim largest As Double = rainfall(0)
    Dim smallest As Double = rainfall(0)
    Dim keepGoing As String = "yes"


    'main program is encapsulated in a while loop 
    'so that user can choose to use program again or exit
    While keepGoing = "yes" Or keepGoing = "Yes"
        'program intro text
        Call programIntro()

        '***********main program module********************************
        Call rainfallProgram(index, SIZE, month, rainfall, total, average, largest, smallest)

        Console.WriteLine("  ")
        Console.WriteLine("  ")
        Console.WriteLine("Would you like to use RainfallAverage again?")
        Console.Write("Enter 'yes' to keep going or any other key to exit:  ")
        keepGoing = Console.ReadLine()
        Console.WriteLine("  ")
        Console.WriteLine("  ")
    End While
End Sub

Sub rainfallProgram(ByRef index, ByVal SIZE, ByVal month, ByRef rainfall,
                    ByRef total, ByRef average, ByRef largest, ByRef smallest)
    'input months and rainfall data in parallel arrays
    For index = 0 To SIZE - 1
        Console.Write("Enter rainfall for " & (month(index)) & ":  ")
        rainfall(index) = (Console.ReadLine())
        Console.WriteLine(" ")
    Next

    Call getTotalRainfall(index, SIZE, rainfall, total)

    Call getAverageRainfall(average, total)

    Console.WriteLine(" ")
    ' ******First problem occurs here
    Console.WriteLine("The largest amount of rainfall was " & (CDbl(getLargestRainfall(index, SIZE, rainfall, largest))) & " inches")
    Console.WriteLine("     and occurred durring" & month(index))

    Console.WriteLine(" ")
    '**********Second Problem occurs here 
    Console.WriteLine("The smallest amount of rainfall was " & (CDbl(getSmallestRainfall(index, SIZE, rainfall, smallest))) & " inches")
    Console.WriteLine("     and occurred durring" & month(index))


    ' Call functionSmallestRainfall()
End Sub

'module for total rainfall
Sub getTotalRainfall(ByVal index, ByVal SIZE, ByVal rainfall, ByRef total)
    For index = 0 To SIZE - 1
        total = total + rainfall(index)
    Next
    Console.WriteLine(" ")
    Console.WriteLine("Total Rainfall:  " & total & " inches")
    index = index + 1
End Sub

'module for average rainfall
Sub getAverageRainfall(ByRef average, ByRef total)
    average = (total / 12)
    Console.WriteLine(" ")
    Console.WriteLine("Average Rainfall:  " & average & " inches")
End Sub

'function for largest rainfall
'********** Another Problem occurs here 
Function getLargestRainfall(ByVal index As Integer, ByVal SIZE As Integer, ByVal rainfall() As Double, ByRef largest As Double) As Double
    For index = 0 To rainfall.Length - 1
        If rainfall(index) > largest Then
            rainfall(index) = largest
        End If
    Next
    Return largest
End Function

'function for smallest rainfall
 '********** Another Problem occurs here 
Function getSmallestRainfall(ByVal index As Integer, ByVal SIZE As Integer, ByVal rainfall() As Double, ByRef smallest As Double) As Double
    For index = 0 To rainfall.Length - 1
        If rainfall(index) > smallest Then
            rainfall(index) = smallest
        End If
    Next
    Return smallest
End Function

'program intro text display
Sub programIntro()
    Console.WriteLine(" ")
    Console.WriteLine("---------------------------------------------------------")
    Console.WriteLine("************   Welcome to RainfallAverage   ************")
    Console.WriteLine("---------------------------------------------------------")
    Console.WriteLine("                                                      ")
    Console.WriteLine("     This program will help to total, average, ")
    Console.WriteLine("       and compare the rainfall of 12 months    ")
    Console.WriteLine(" ")
    Console.WriteLine("      Unit is inches. Input numeric values only")
    Console.WriteLine(" ")
    Console.WriteLine(" ")
End Sub

端模块

4

1 回答 1

3

鉴于这是一项任务(感谢您的诚实),我不会给您完整的解决方案,但我会指出我看到的两个问题,本着帮助您自己解决其余问题的精神:-)

Function getSmallestRainfall(ByVal index As Integer, ByVal SIZE As Integer, ByVal rainfall() As Double) As Double
    Dim smallest = rainfall(0) 
    For index = 0 To rainfall.Length - 1
        If rainfall(index) < smallest Then
            smallest = rainfall(index) 
        End If
    Next
    Return smallest
End Function

我已经更改了您的 if 语句,以便在必要时执行分配,即当您达到比您已经访问过的值更小的值时(rainfall(index)< minimum)。我还更改了赋值语句。您更改了雨量数组的那个,以便删除其当前检查的元素并替换为当前最小的雨量值。= 是一个破坏性的更新运算符,而不是使左右手相等的东西。所以你必须知道你正在改变哪些价值,你要保持哪些价值。您将要更新的内容放在左侧,将其新值放在右侧(例如 x = 5 将值 5 分配给 x)。

希望有帮助!

于 2012-11-29T18:30:24.797 回答