我是编码和VB的新手。为了额外的学分,我们的老师希望我们模块化一个项目。
我将如何开始。它会像在代码中添加avg()一样简单吗?
'GradesAverageFoose.vb - This program calculates the average grades of 3 grades. Sep 11, 2017 by Christopher Foose v1
'Display: Grades
' Input: 3 different grades
' Process: take the three grades, add together and divide answer by 3
' Output: Average
'Created by Christopher Foose
Option Explicit On
Option Strict On
Module grades
' Declarations
Dim gradeString1 as string
Dim gradeString2 as string
Dim gradeString3 as string
Dim grade1 as double = 0
Dim grade2 as double = 0
Dim grade3 as double = 0
Dim average as double = 0
Sub Main()
housekeeping()
avg()
Endofjob()
end sub
' This is the work done in the housekeeping() procedure
sub housekeeping()
gradeString1 = InputBox$("Enter Grade 1: ")
grade1 = Convert.ToDouble(gradeString1)
gradeString2 = InputBox$("Enter Grade 2: ")
grade2 = Convert.ToDouble(gradeString2)
gradeString3 = InputBox$("Enter Grade 3: ")
grade3 = Convert.ToDouble(gradeString3)
end sub
average = (grade1 + grade2 + grade3) /3
' Output area
System.Console.WriteLine("Average is " & average )
End Sub
End Module ' End of grades Module
此外,我希望在正确的方向上轻推,而不是一个平坦的解决方案。这是为了学校,我现在正在学习。
答案 0 :(得分:-1)
在任何您看到重复文本的地方保存变量,将其旋转为函数。当然,我对vb.net更熟悉....
Dim average as double = housekeeping(3)
'Output area
System.Console.WriteLine("Average is " & average )
function housekeeping(GradeCount as Integer) as Double
Dim GradeSums as Double = 0
For value As Integer = 1 To GradeCount
Dim GradeValue = InputBox$(string.format("Enter Grade {0}: ",value)
GradeSums = GradeSums + Convert.ToDouble(GradeValue)
Next
return GradeSums / GradeCount
End function