字符串通过隐藏和显示进行操作

时间:2011-12-26 15:48:43

标签: vb.net string

这个想法很简单,如果我有一个字符串值“ABCD”然后使用ButtonClick事件,它应该随机显示一个字符,而其他人则隐藏。即,“ B *”另一次点击会“AB **”等等。

到目前为止,我被困在for循环中。

    For Each c As Char In x
        y = Random.Next(0, x.IndexOf(c))
    Next

我仍然在这个阶段学习VB.NET。

2 个答案:

答案 0 :(得分:1)

Public Class Form1

    Private _indexes As Integer()
    Private _currentIndex As Integer
    Private _chars As Char()
    Private _template As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim rnd = New Random()

        _template = "ABCD"

        ' Create indexes that are randomly sorted 
        _indexes = Enumerable _
            .Range(0, _template.Length) _
            .OrderBy(Function(i) rnd.Next()) _
            .ToArray()

        'Create an array of chars with stars '****'. 
        _chars = New String("*"c, _template.Length).ToCharArray()

        _currentIndex = 0
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If _currentIndex < _template.Length Then
            Dim index As Integer = _indexes(_currentIndex)
            _currentIndex += 1
            _chars(index) = _template(index)
            Dim result As String = New String(_chars)
            Label1.Text = result
        End If
    End Sub

End Class

答案 1 :(得分:0)

我已经发布了一个答案,我专注于算法。算法代码直接集成到表单中。这有效,但不是一个好习惯。代码将更具可重用性,更易理解,并且如果将其提取到单独的类中,则可以更轻松地进行测试。

Public Class RandomTextRevealer
    Private _indexes As Integer()
    Private _currentIndex As Integer
    Private _chars As Char()
    Private _template As String
    Private _result As String

    Public Sub New(ByVal templateText As String)
        Dim rnd = New Random()

        _template = templateText

        ' Create indexes that are randomly sorted 
        _indexes = Enumerable _
         .Range(0, _template.Length) _
         .OrderBy(Function(i) rnd.Next()) _
         .ToArray()

        'Create an array of chars with stars '****'. 
        _chars = HiddenText.ToCharArray()

        _currentIndex = 0
    End Sub

    Public Function RevealNext() As String
        If _currentIndex < _template.Length Then
            Dim index As Integer = _indexes(_currentIndex)
            _currentIndex += 1
            _chars(index) = _template(index)
            _result = New String(_chars)
        End If
        Return _result
    End Function

    Public ReadOnly Property HiddenText() As String
        Get
            Return New String("*"c, _template.Length)
        End Get
    End Property
End Class

我们可以在一个小的控制台应用程序中测试这样的类,没有表单:

Module Programm
    Public Sub Main()
        Dim revealer = New RandomTextRevealer("Just a test")
        Console.WriteLine(revealer.HiddenText)

        For i As Integer = 1 To 12
            Console.WriteLine(revealer.RevealNext())
        Next
        Console.ReadKey()
    End Sub
End Module

现在我们可以将它集成到这样的形式中(我添加了一个Reset-button,以便能够使用不同的随机值重复测试):

Public Class Form2
    Dim revealer As RandomTextRevealer

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Reset()
    End Sub

    Private Sub btnReveal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReveal.Click
        Label1.Text = revealer.RevealNext()
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Reset()
    End Sub

    Private Sub Reset()
        revealer = New RandomTextRevealer("ABCD")
        Label1.Text = revealer.HiddenText
    End Sub
End Class

现在我们的表单代码看起来更清晰。