本文介绍了如何在同一表单中创建两个单独的随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将数字添加游戏作为计算机科学项目的一部分,并使用我目前拥有的代码在不同的文本框中显示相同的数字.我想在不同的框中显示两个不同的随机数,谢谢.

trying to make a number adding game as part of a computer science project and with the code i currently have it displays the same number in different text boxes. I would like two different random numbers displayed in separate boxes, Thanks.

    Dim rn As Random
    Dim number As Integer
    rn = New Random
    number = rn.Next(1, 9)
    Dim rn2 As Random
    Dim number1 As Integer
    rn2 = New Random
    number1 = rn2.Next(1, 9)



    tbxOne.Text = number.ToString
    tbxTwo.Text = number1.ToString

End Sub

推荐答案

您可以使用 1 个实例本身创建随机数.此外,您的随机数可以不同或相同.

You can create random numbers with 1 instance itself. Moreover your random numbers can be different or same.

Dim rn As Random
Dim number As Integer
Dim number1 As Integer

rn = New Random
number = rn.Next(1, 9)
number1 = rn.Next(1, 9)

tbxOne.Text = number.ToString
tbxTwo.Text = number1.ToString

这篇关于如何在同一表单中创建两个单独的随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:33