用于随机数生成器的Visual Basic文本框数组 [英] Visual Basic Text Box Array for random number generator

查看:91
本文介绍了用于随机数生成器的Visual Basic文本框数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个随机数生成器.这是学校功课.我似乎无法正常工作.

I'm wanting to make a random number generator. It's for school homework. I just can't seem to get it to work.

任务是使一个随机数生成器在20个文本框中生成20个数字,以问10个数学问题.

The task was to make a random number generator to generate 20 numbers into 20 text boxes to ask 10 maths questions.

它必须尽可能高效.我需要帮助,因为我只能这样做,但是它效率很低而且代码很长.

It needs to be as efficient as possible. I need help as I can only do it but it's really inefficient and really long code.

Dim RandomNumberAHigh As Integer = 12 'Here I am declaring my highest number for the random number generator to use. This will be the highest number the maths quiz will ask the students in the questions.
Dim RandomNumberALow As Integer = 1 'Here I am declaring my lowest number for the random number generator to use. This will be the lowest number the maths quiz will ask the students in the questions.
Dim Random As Integer = 0 'Her I am declaring a variable that will be used to insert the numbers from the random number generator in the quiz text boxes.
Randomize()
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd1.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd4.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd7.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd10.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd13.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd16.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd19.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd22.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd25.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd28.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd3.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd6.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd9.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd12.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd15.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd18.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd21.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd24.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd27.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd30.Text = (Random)

推荐答案

尝试对所有文本框使用foreach循环,如下所示:

Try to use a foreach loop for all textboxes as below:

Dim RandomNumberAHigh As Integer = 12
Dim RandomNumberALow As Integer = 1
Dim Random As Integer = 0
For Each txt As Control In Me.Controls
    If TypeOf txt Is TextBox Then
        txt.Text = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
    End If
Next

这篇关于用于随机数生成器的Visual Basic文本框数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆