2014-01-12 85 views
1

我正在研究一个大学项目,在这个项目中我必须创建一个程序,它存储了20个问题和答案,然后一个接一个地显示在一个教师窗体上(点击下一个按钮)放在学生表格上。VB.net中的数组混淆

我遇到的问题是我可以输入问题和答案(数组0到19),但是当学生回答问题时,只显示19个问题,而最后一个问题没有出现。

让我知道我可以告诉你什么来帮助解决我的问题。

Module Module1 
    Public myQ(0 To 19) As String 
    Public myA(0 To 19) As String 
End Module 


Public Class frmTeacher 
Public myCounter As Integer 


Private Sub frmTeacher_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    myCounter = (0) 
End Sub 


Private Sub btnTeacherNext_Click(sender As Object, e As EventArgs) Handles btnTeacherNext.Click 

    If myCounter < 19 Then 

     myQ(myCounter) = txtTeacherQ.Text 
     myA(myCounter) = txtTeacherA.Text 
     myCounter = myCounter + 1 

     txtTeacherQ.Text = "" 
     txtTeacherA.Text = "" 
    Else 
     MsgBox("20 Questions Created, Moving on To Student Screen") 
     Me.Hide() 
     frmStudent1.Show() 
    End If 
End Sub 

Public Class frmStudent1 
    Dim myScore As Integer 
    Dim MyCounter2 As Integer 
    Public myNames As String 
    Private Sub btnStudentHelp_Click(sender As Object, e As EventArgs) Handles btnStudentHelp.Click 
     MsgBox("Questions will be shown to the left, Place your answer into the box on the right and click next") 
    End Sub 


    Private Sub frmStudent1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     lblStudentQ.Text = myQ(0) 
     HideItAll() 
     txtStudentName.Visible = True 
     btnStart.Visible = True 
    End Sub 
    Private Sub HideItAll() 
     lblStudentQ.Visible = False 
     txtStudentA.Visible = False 
     txtStudentName.Visible = False 
     btnStudentHelp.Visible = False 
     btnNextStudent.Visible = False 
     btnStart.Visible = False 
     btnStudentNext.Visible = False 

    End Sub 

    Private Sub btnStudentNext_Click(sender As Object, e As EventArgs) Handles btnStudentNext.Click 

     If MyCounter2 < 19 Then 
      If txtStudentA.Text = myA(MyCounter2) Then 
       myScore = myScore + 1 
      End If 
      MyCounter2 = MyCounter2 + 1 
      lblStudentQ.Text = myQ(MyCounter2) 


     Else 

      MsgBox("Your score is " + Str(myScore)) 
      myNames = myNames + txtStudentName.Text + ": " + Str(myScore) + vbNewLine 
      HideItAll() 
      btnNextStudent.Visible = True 
     End If 
    End Sub 

    Private Sub btnNextStudent_Click(sender As Object, e As EventArgs) Handles btnNextStudent.Click 
     Me.Refresh() 
     HideItAll() 
     txtStudentName.Visible = True 
     btnStart.Visible = True 
    End Sub 

    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click 
     myScore = 0 
     MyCounter2 = 0 
     If txtStudentName.Text = "teacher" Then 
      MsgBox("The scores are as follows: " + vbNewLine + myNames) 
     End If 
     HideItAll() 
     lblStudentQ.Visible = True 
     txtStudentA.Visible = True 
     btnStudentHelp.Visible = True 
     btnStudentNext.Visible = True 
    End Sub 
End Class 
+5

你应该补充一点,初始化代码失败该数组和代码显示下一个问题/回答 – Steve

+0

编辑第一篇文章:)将尝试并修复它的格式,虽然 – ConfusedStudent

回答

0

当你宣布你的数组作为

Public myQ(0 To 19) As String 

您已经有效地创建了一个可以托管20个字符串的数组,它们的索引最大为0到19。这意味着你的myCounter2变量可以具有最大值19,否则你会超出数组的范围。此时如果你只显示问题,并且有一个指标少的答案,同时,有19你失去了最后几个问题/回答

你应该使用这种条件

If MyCounter2 < myQ.Length Then 
..... 

Array.Length解决您的代码返回可以存储在数组的所有维中的元素的数量(20)。
使用这个属性更好,因为如果你改变数组的大小(比如说你想问40个问题得到40个答案),那么你不需要检查代码的每一行来调整出现在任何地方的幻数。

但是,变更后,该行的btnStudentNext_Clicks事件时MyCounter2值是19

' MyCounter2 = 19 + 1 
    MyCounter2 = MyCounter2 + 1 

    ' this fails because at this point MyCounter2 is 20 
    lblStudentQ.Text = myQ(MyCounter2) 

你的代码的可能重构可能是

Private Sub btnStudentNext_Click(sender As Object, e As EventArgs) Handles btnStudentNext.Click 

    If MyCounter2 < myQ.Length Then 
     If txtStudentA.Text = myA(MyCounter2) Then 
      myScore = myScore + 1 
     End If 
     MyCounter2 = MyCounter2 + 1 
    End if 

    if MyCounter2 >= myQ.Length Then 
     ShowResults(); 
    else    
     lblStudentQ.Text = myQ(MyCounter2) 
     txtStudentA.Text = "" 
    Endif 
End Sub 

Private Sub ShowResults() 
    MsgBox("Your score is " + Str(myScore)) 
    myNames = myNames + txtStudentName.Text + ": " + Str(myScore) + vbNewLine 
    HideItAll() 
    btnNextStudent.Visible = True 
End Sub 
+0

那就是突出显示为超出数组界限的那一行,有没有办法解决这个问题? – ConfusedStudent

+0

是刷新答案.... – Steve

+0

谢谢你生病了试试这个,回复你 – ConfusedStudent

2

改变这一点:

If MyCounter2 < 19 Then 

到:

If MyCounter2 < 20 Then 

或:

If MyCounter2 <= 19 Then 
+0

嗯现在我得到索引超出数组的界限。 – ConfusedStudent