2014-06-08 57 views
-1

我目前在大學的IT課程中。高級Visual Basic 2010是一個需求,但是,我不是程序員。我一直在努力尋找通過VB的方式,但最後一項任務讓我難以接受。我能夠將名字寫入數組中,併爲該名字取得5個等級。此時,循環將繼續詢問下一個名稱,並且名稱等級爲5個等級,直到輸入第4個名稱和等級,然後它應該在列表框中顯示所有4個名稱和等級平均值。年級排列平均練習題

這裏是分配...

編寫一個程序,將輸入四位同學的名字和平均五個測試成績爲每個學生。該程序應該有一個用於學生姓名的數組,然後是所有成績的二維數組。 你的程序應該要求學生的姓名,然後要求該學生的五個考試成績。 創建一個平均值並將數組傳遞給該方法的方法。該方法還可以在列表框中輸出學生姓名和平均值。 調用一種方法來計算一次獲得所有成績後的平均值。當你獲得信息時不要弄清楚它!如果你這樣做,你會得到一個大零!然後用相同的方法將結果輸出到列表框中:

經過4天的努力,這是我迄今爲止提出的。任何指導非常感謝。先謝謝你。

Public Class Form1 

    Private Sub btnNames_Click(sender As System.Object, e As System.EventArgs) Handles btnNames.Click 
     Dim NamesList(3) As String 

     Dim GradeArray(4) As Integer 
     Dim x As Integer 
     Dim y As Integer 
     Dim Sum As Integer 
     Dim Avg As Integer 

     For y = 0 To NamesList(3) 
      NamesList(x) = InputBox("Enter student number " & y + 1 & "'s name:", "Enter a name") 
     Next 

     For y = 0 To GradeArray.Length - 1 
      GradeArray(y) = InputBox("Enter grade number " & y + 1 & " for " & NamesList(0) & " in the box:", "Enter the grades") 
     Next 

     For Each item In GradeArray 
      Sum = Sum + item 
     Next 

     Avg = Sum/5 

     lstAverages.Text = Avg.ToString 

    End Sub 

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click 
     Me.Close() 
    End Sub 
End Class 
+1

請給1(一)** **具體問題'這裏是什麼我到目前爲止已經不是問題了。我們不會爲你做這件事,如果你沒有詳細說明,你就不知道你被困住了。也就是說,它會要求3名學生,但只有一組成績。設置一個斷點並觀察每條線的執行情況,並且您將瞭解到發生什麼事情以及您會發生什麼**。 – Plutonix

+0

除了@Plutonix所說的,你錯過了一個2維數組(每個賦值),而'For y = 0 To NamesList(3)'就是屬於「what發生在你想**會發生的事情上「。 – Tim

+5

提示:設置'Option Strict On'(總是),你會學到更多,運行時錯誤更少,編寫更好的代碼並且始終具有這種剛剛洗過的感覺。 – Plutonix

回答

0

我沒有別的事情好做,所以我接受了給它一試......這還包括每個你說:4級的學生 - 每5個等級,排列學生的名字和一個二維數組保持所有成績。有一種方法將這些傳遞給它並執行學生成績的平均值,然後按要求將它們吐到列表框中。快樂編碼!

P.S.我沒有做任何錯誤處理或者,你可能想補充一點,或者至少實現了某些東西來處理這樣的情況下...

Public Class Form1 

Private arrStudents(3) As String 'Student's name array (4) 
Private arrScores(3, 4) As Integer 'Students scores (5 each) 

'Start getting the data we need 
Private Sub btnGetStudents_Click(sender As Object, e As EventArgs) Handles btnGetStudents.Click 
    Dim strStudent As String = String.Empty 
    Dim intScore As Integer = 0 
    Dim intPlace As Integer = 0 

    'Get the students name... 
    For i As Integer = 0 To arrStudents.Length - 1 
     Do Until strStudent <> String.Empty 
      strStudent = InputBox("Please enter student's name: ", "Gather Student Grades") 
     Loop 
     arrStudents(i) = strStudent 

     'Reset our variable... 
     strStudent = String.Empty 

     'Get the students grades... 
     For s As Integer = 0 To arrScores.Length - 1 

      Do Until intScore > 0 
       intScore = CInt(InputBox("Please enter student's scores: ", "Gather Student Scores")) 
      Loop 

      If (intPlace = 4 AndAlso i = arrStudents.Length) Then 
       intPlace = 0 
       arrScores(i, s) = intScore 
       intScore = 0 
      ElseIf intPlace = 4 Then 
       intPlace = 0 
       arrScores(i, s) = intScore 
       intScore = 0 
       Exit For 
      Else 
       arrScores(i, intPlace) = intScore 
       intPlace += 1 
      End If 

      'Reset our variables... 
      intScore = 0 
     Next 
    Next 

    'Calculate and output the data to the listbox... 
    GetStudentAverages(arrStudents, arrScores) 

End Sub 

'Function to average per student grades and then display them all in the listbox... 
Private Sub GetStudentAverages(ByVal arStudent As Array, ByVal arScore As Array) 
    Dim strStudentData As String = String.Empty 
    Dim intAverage As Integer = 0 
    Dim intPlace As Integer = 0 

    'Start averaging the students scores and then add them to the listbox... 
    For i As Integer = 0 To arStudent.Length - 1 
     For g As Integer = 0 To arScore.Length - 1 
      If intPlace = arStudent.Length Then 
       intAverage += arScore(i, intPlace) 
       Exit For 
      Else 
       intAverage += arScore(i, intPlace) 
       intPlace += 1 
      End If 
     Next 
     intAverage = CInt(intAverage/5) 
     intPlace = 0 

     'Output the student information... 
     ListBox1.Items.Add("Student: " & arStudent(i).ToString & " Average: " & intAverage.ToString) 
     intAverage = 0 
    Next 
End Sub 

End Class 
+0

可以解釋downvote嗎?它有助於爲什麼,這樣我可以證明什麼是錯的,謝謝。 – Codexer

+0

非常感謝任何花點時間看我的請求的人。特別感謝CoDeXeR先生。我很欣賞這項請求的工作。這有很大幫助。我不得不做一些小的調整,但沒有你的意見,我仍然會掙扎。 – Manolo

+0

您的歡迎,很高興我能提供幫助。 – Codexer