【问题标题】:finding the next empty space in an array在数组中查找下一个空白空间
【发布时间】:2018-06-23 11:06:22
【问题描述】:

我在尝试将玩家的姓名和得分加载到数组的第一个开放空间时出现错误,但它会在数组中一遍又一遍地创建该名称的重复项。在此先感谢

 Structure Player
    Dim Name As String
    Dim Score As Integer
End Structure

Public HighScores(100) As Player


 For i = 0 To 99
            If HighScores(i).Name = "" And HighScores(i).Score = 0 Then
                HighScores(i).Name = PlayerName
                HighScores(i).Score = CurrentScore
            Else
                i += 1
            End If
        Next

【问题讨论】:

  • 我不明白创建重复名称是什么意思。请多解释一下。
  • 所以它会将玩家姓名和当前得分变量放在数组的多个位置。像 Array(0) 和 Array(1) 和 Array(2) 都将是相同的名称和分数。
  • 我建议使用 Dictionary(Of String,Integer) 或 List(Of Player) 并在添加之前检查对象中是否已经存在玩家名称。
  • 我会在设置 IF 块中的值后添加“退出”。这就是为什么你会得到多个相同值的条目。另外,删除你的 else 块。 for 循环区域的索引 i
  • 你能举一个Exit For代码的例子吗?你的意思是在 THEN 之后?

标签: arrays vb.net


【解决方案1】:

您当前的代码将在它找到的每个空索引中设置提供的值。找到空索引并设置它的值后,您需要停止设置值(退出循环)。

For i = 0 To 99

   If HighScores(i).Name.Length = 0 AndAlso HighScores(i).Score = 0 Then 'Determines if the index is empty

       HighScores(i).Name = PlayerName
       HighScores(i).Score = CurrentScore   'Sets the values

       Exit For    'Exits the loop

    End If

Next

如果第一个索引符合您的要求,上面的代码只会在循环中运行一次,如果第一个索引不符合您的要求,则运行两次,但第二个符合,依此类推。

【讨论】:

    【解决方案2】:

    如 David Wilson 在 cmets 中建议的那样,为了避免遍历数组以寻找空槽,请使用 List(Of T)。 T 代表 Type,Player 是 Type。这会将您的列表限制为仅包含 Player 类型的对象。我包括了一些使用 LINQ to Objects 对列表进行排序的内容。有在线cmets。尽管我认为列表会是更好的方法,但白狼对 Exit For 的回答应该可以解决您的问题。

    Structure Player
            Public Score As Integer
            Public Name As String
            'Added a constructor to the structure to make it easy to add new Player
            Public Sub New(myScore As Integer, myName As String)
                Score = myScore
                Name = myName
            End Sub
        End Structure
        Private lstScores As New List(Of Player)
        Private Sub BuildList()
            lstScores.Add(New Player(500, "Mathew"))
            lstScores.Add(New Player(200, "Mark"))
            lstScores.Add(New Player(300, "Luke"))
            lstScores.Add(New Player(700, "John"))
            'Sort and display list
            SortList()
        End Sub
        Private Sub AddScore(strName As String, intScore As Integer)
            lstScores.Add(New Player(intScore, strName))
        End Sub
        Private Sub SortList()
            'Note: the original lstScores is not changed
            Dim orderedList = From scorer In lstScores Order By scorer.Score Descending Select $"{scorer.Score} - {scorer.Name}"
            'orderedList is an IEnumerable(Of String) because the Select part of the LINQ query is a string.
            'Using .ToList provides the DataSource of the ListBox with the formatted strings
            'Display the sorted list in a list box
            ListBox1.DataSource = orderedList.ToList
        End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      相关资源
      最近更新 更多