如何从一组2中随机选择一个单词并显示它?

时间:2013-06-02 13:49:52

标签: vb.net

这是一个控制台应用程序

如何随机显示文字,但我已将其添加到程序中?

示例:我正在进行正面或反面比赛。我想做到这一点,你输入你的选择(头/尾),它随机说“头”或“尾巴”,在下一行,它根据匹配说“你赢了”或“输了”。

我只是不知道如何让它从程序中随机选择文本并显示它。

1 个答案:

答案 0 :(得分:1)

像这样的东西

Dim prng As New Random
Dim done As Boolean = False

Sub Main()
    Do
        Dim toss As Integer = prng.Next(2) '0=heads, 1=tails
        Console.WriteLine(Environment.NewLine & "Enter (h)eads, (t)ails, or e(x)it")
        Dim inp As String = Console.ReadLine.ToLower
        Dim choice As Integer
        Select Case inp
            Case "h", "head", "heads"
                choice = 0
            Case "t", "tail", "tails"
                choice = 1
            Case "x", "exit"
                choice = -1
                done = True
            Case Else
                choice = 2 'input error
        End Select
        If choice = toss Then
            Console.WriteLine("Winner, winner, chicken dinner!")
        ElseIf choice = 2 Then
            Console.WriteLine("Input error, try again")
        ElseIf choice = -1 Then
            'exit
        Else
            Console.WriteLine("You lose")
        End If
    Loop While Not done
End Sub