![]() |
Click here to advertise with us
|
|
|||||||
| VB.NET General Discussion VB.NET general discussion area |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Hi,
I'm very new to programming and i'm trying to create a word jumbler. The idea is someone types something in to a text box and a label displays the letters in a random order. I'm trying to use an array to ensure the same value doesn't appear twice can someone tell me where i'm going wrong? Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rndNumber As Integer
Dim arrRepeat(TextBox1.Text.Length) As Integer
Dim store, txtCount, i As Integer
Label2.Text = " "
For txtCount = 1 To TextBox1.Text.Length
start: Randomize()
rndNumber = Int((Rnd()) * TextBox1.Text.Length)
For store = 0 To TextBox1.Text.Length - 1
If rndNumber = arrRepeat(store) Then
GoTo start
End If
arrRepeat(txtCount) = rndNumber
Next store
Next txtCount
For i = 0 To TextBox1.Text.Length - 1
Label2.Text = Label2.Text & TextBox1.Text.Chars((arrRepeat(i) + 1))
Next
End Sub
|
|
|||
|
I'm hoping this isn't for a class but it does look like you've tried to research this (a little).
Code:
Imports System.Text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim WordArray() As String = Split(TextBox1.Text, " ")
Dim Word() As Char
Dim LetterPlaceholder As Char
Dim WordCount As Integer
Dim i As Integer
Dim Position As Integer
Dim sb As New StringBuilder()
For WordCount = 0 To WordArray.Length - 1 'Cycle through array of strings
If WordCount > 0 AndAlso WordCount <= WordArray.Length - 1 Then
sb.Append(" ") 'Add space between word
End If
Word = WordArray(WordCount)
For i = 0 To Word.Length - 1 'Cycle through each letter in a word
Position = (Word.Length - 1) * Rnd() 'Swap with random position in string
LetterPlaceholder = Word(Position) 'Store char to swap so it doesn't get lost
Word(Position) = Word(i) 'Set current letter to with letter to swap
Word(i) = LetterPlaceholder 'Set current letter with swapped letter
Next
sb.Append(Word) ' Add swapped word to string builder
Next
TextBox2.Text = sb.ToString()
End Sub
End Class
|
|
||||
|
another suggestion...
Code:
Private Sub JumbleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JumbleButton.Click
Me.ResultLabel.Text = RandomizeWord(Me.InputTextBox.Text)
End Sub
Private r As New Random
Private Function RandomizeWord(ByVal word As String) As String
Dim newword As String = String.Empty
For Each c As Char In word
newword = newword.Insert(r.Next(0, newword.Length + 1), c)
Next
Return newword
End Function
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|