如何生成随机字母(仅限元音)

时间:2013-10-18 09:09:00

标签: vb.net

我当前的代码

  

Public Class Form1

     

Dim randomObject As New Random()

Dim alphaRand As Integer = randomObject.Next(65, 91)

 Dim alpha As String = Me.textAlphabet.Text.ToUpper

    Dim asciicode As Integer = Asc(alpha)

    If asciicode = alphaRand Then
        Me.lblAlphaResult.Text = "Congratulation! Your guess:  " & textAlphabet.Text & " is correct,you win"
        Me.cmdAlphaNewGame.Enabled = True
        Me.cmdAlphaGuess.Enabled = False
    ElseIf asciicode < alphaRand Then
        Me.lblAlphaResult.Text = "You guess is too low.Try again"
    ElseIf asciicode > alphaRand Then
        Me.lblAlphaResult.Text = "Your guess is too high.Try again"
    End If
End Sub
     

结束班

* randomObject.Next(65,91)'这意味着它只根据asciicode生成随机alpha到AZ,只有元音呢? *

我可以使用类似数组的东西吗? Dim元音As String()= {“A”,“E”,“I”,“O”,“U”} 然后从我的字符串生成随机字母表 让我稍后猜测

2 个答案:

答案 0 :(得分:3)

您可以执行类似

的操作
Dim vowels As String() = {"A","E","I","O","U"}
Dim i As Int32 = randomObject.Next (0, 5)
Return vowels(i)

答案 1 :(得分:0)

或只是使用字符串

Dim randomObject As New Random
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim vowels As String = "AEIOU"

    Dim vowelpicked As String

    vowelpicked = vowels.Substring(randomObject.Next(vowels.Length), 1)

    Debug.Write(vowelpicked)
End Sub