2015-04-02 113 views
1

編碼新手。上週我有一個項目要求用戶輸入4個等級,然後計算所有4個等級的平均值,並且最低等級的平均值下降(已經轉換成btw)。要求是For Next循環和Do While循環來計算平均值。我使用For Next循環來獲得4個等級的總和,然後計算平均值。需要VB做... while循環來計算平均值

Declare variables, constant, and array 
     Const intMAX_SUBSCRIPT As Integer = 3 
     Dim intGrades(intMAX_SUBSCRIPT) As Integer 
     Dim intTotal As Integer = 0  'holds the total of grades 
     Dim dblAverage As Double  'holds the average of all 4 grades 
     Dim intCount As Integer   'loop counter 
     Dim intLowest As Integer  'holds the lowest score 

     'assign grades to array slots 
     intGrades(0) = CInt(txtGrade1.Text) 
     intGrades(1) = CInt(txtGrade2.Text) 
     intGrades(2) = CInt(txtGrade3.Text) 
     intGrades(3) = CInt(txtGrade4.Text) 

     'Loop to calculate average in grades array 
     'get total of all grades 
     For intCount = 0 To (intGrades.Length - 1) 
      intTotal += intGrades(intCount) 

     Next 
     'use floating-point div to find average 
     dblAverage = intTotal/intGrades.Length 

這工作正常。然後我用以下找到最低年級:

 intLowest = intGrades(0) 

     'Search for the lowest grade in the array 
     For intCount = 1 To (intGrades.Length - 1) 
      If intGrades(intCount) < intLowest Then 
       intLowest = intGrades(intCount) 
      End If 
     Next 

這也工作,因爲它應該。現在,我遇到了這個問題:做一個儘管計算新的平均值與最低的分數下降。這是我需要幫助的地方:

 Dim intNewTotal As Integer = 0 'holds the new total of the 3 highest grades 
     Dim dblNewAverage As Double 'holds the new average with the lowest score dropped 
     Dim intNewCount As Integer = 0 'loop counter 

     Do While intNewTotal <= (intTotal - intLowest) 
      intNewTotal = intNewTotal + intGrades.Length - 1 
      intNewCount += 1 
     Loop 

     dblNewAverage = intNewTotal/3 

數學很接近但並不完全。對於100,100,80和100的分數,我得到以下結果:所有4的平均值= 95,最低分數下降的平均值= 101.新的平均值總是比它應該高一個。我能做些什麼來解決這個問題?我真的很想理解,所以如果它再次發生,我可以解決這個問題。謝謝!

+0

如果通過與調試器單步執行代碼(DO上設置一個斷點雖然intNewTotal <=(intTotal - intLowest)),你會看到你的價值之一是他們應該不是什麼。從那裏倒退。 – Mathemats 2015-04-02 01:38:00

回答

1

這可能是家庭作業,在這種情況下,你不會明白無論如何。但如果你真正想學習一親會怎麼做,代碼看起來會像這樣:

Dim Grades As New List(Of Integer) From {CInt(txtGrade1.Text), CInt(txtGrade2.Text), CInt(txtGrade3.Text), CInt(txtGrade4.Text)} 

Dim Total As Integer = Grades.Sum() 
Dim Average As Double = Grades.Average()  'holds the average of all 4 grades 
Dim Lowest As Integer = Grades.Min()  'holds the lowest score 
Dim NewAverage As Double = CDbl(Total - Lowest)/(Grades.Count - 1) 

拖放您的變量名傻類型的前綴。在Option Strict是默認設置之前,他們曾經很流行並且很有意義,但現在甚至微軟自己的風格指南都建議不要這樣做。

+0

MS編碼標準推薦用於局部變量名稱的camelCase,所以這些應該命名爲'total''newAverage'等。 – 2015-04-02 07:05:37

+0

這就是我們通常這樣做的方式,但OP指定他需要使用Do While循環來計算平均值。我想作業的目的是強調循環,而不是平均本身。 – 2015-04-02 11:39:10

+0

我的導師需要前綴。我同意:他們很愚蠢。我喜歡這個解決方案,但它不適合這個年級。這比我所有的胡言亂語都更有意義。 – tanker405th 2015-04-02 17:44:18

1

的問題是這行代碼:

intNewTotal = intNewTotal + intGrades.Length - 1 

取而代之的將等級值,你加intGrades數組的長度,直到到達做,當條件:intNewTotal <= (intTotal - intLowest)

我明白你的問題是爲了學術目的,所以如果你真的需要使用Do While循環來計算平均值,我會以這種方式使用它(未經測試):

Dim index As Int = 0 
Do While index <= intMAX_SUBSCRIPT 

    If (intGrades(index) <= intLowest) Then 
    intNewTotal += intGrades(index) 
    intNewCount += 1  
    End if 

    index += 1 

Loop 

dblNewAverage = intNewTotal/intNewCount 

編輯

刪除最低年級可能會被解釋爲2種不同的方式。如果您的值爲80,80,100,110,則您可能只想放置其中一個值,或者兩者都是。我上面的代碼正在放棄他們兩個。如果要刪除只是其中之一,那麼你應該開始索引設置爲1,則忽略第一項,刪除if條件:

Dim index As Int = 1 
Do While index <= intMAX_SUBSCRIPT 
    intNewTotal += intGrades(index) 
    index += 1 
Loop 

dblNewAverage = intNewTotal/(intGrades.Length - 1) 
+0

編輯的版本仍然不會返回正確的結果。下降得分最低的平均總計算低於所有四個分數的平均值。我應該用已經在我的程序中的東西來替換索引嗎? – tanker405th 2015-04-02 21:08:07

0
Dim j As New List(Of Int32) 
    Dim sum As Int32 = 0 
    For i = 1 To 4 '4 number of textboxes 
     j.Add(CType(Controls.Item("txtGrade" + i.ToString), TextBox).Text) 
     sum += Val(CType(Controls.Item("txtGrade" + i.ToString), TextBox).Text) 
    Next 
    j.Sort() 
    Dim intLowest As Int32 = j(0) 
    Dim avg As Int32 = sum/j.Count