Visual Basic 数字选择器代码 [英] Visual Basic Number selector code
问题描述
我目前正在用 Visual Basic 开发一个数学游戏,但我需要一些代码集的帮助.
Im currently developing a math game in visual basic but I need some help with a certain set of code.
我需要在代码中选择随机数:Dim Result0 = Rnd1.Next(10, 20)
,总是以 0 或 2 结尾,因此不会选择像 13 和 15 这样的数字.我需要这样做,因为我正在制作一个除法游戏,所以如果出现像 10/6 这样的随机问题,我必须输入一个很长的十进制数字,这很烦人.这是完整的代码.
I need the random numbers selected in the code: Dim Result0 = Rnd1.Next(10, 20)
, to always end in 0 or 2 so numbers like 13 and 15 wont be selected. I need to do this because im making a division game so if a random question like 10/6 comes up I have to enter a long decimal digit which is just annoying. Here is the full code.
Public Class Division
Dim Rnd1 As New Random
Dim Rnd2 As New Random
Dim Result0 = Rnd1.Next(10, 20)
Dim Result1 = Rnd1.Next(1, 10)
Dim Total = Result0 / Result1
Dim Score As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
TextBox1.Text = Result0
TextBox2.Text = Result1
Label3.Text = Total
Label4.Text = Score
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Rnd1 As New Random
Dim Rnd2 As New Random
Dim Result0 = Rnd1.Next(10, 20)
Dim Result1 = Rnd1.Next(1, 10)
Dim Total = Result0 / Result1
If TextBox3.Text = Label3.Text Then
Score += 1
TextBox3.Text = ""
Else
MessageBox.Show("Incorrect")
TextBox3.Text = ""
End If
TextBox1.Text = Result0
TextBox2.Text = Result1
Label3.Text = Total
Label4.Text = Score
End Sub
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
Button1.PerformClick()
End If
End Sub
End Class
推荐答案
您可以做的是在分配所有值之前测试总数是否为整数,如果不是重新生成数字.
What you could do is test if the total is an integer before you assign all the values, and if it isn't regenerate your numbers.
请注意,当您对数字进行除法时,您可以使用/"或\",具体取决于您是否需要整数值.请参阅 https://msdn.microsoft.com/en-us/library/b6ex274z.aspx
Note when you divide numbers you can use "/" or "\" depending on if you want an integer value. See https://msdn.microsoft.com/en-us/library/b6ex274z.aspx
Dim Rnd1 As New Random
Dim Rnd2 As New Random
Dim Result0 As Integer
Dim Result1 As Integer
Dim Total As Integer
Do
Result0 = Rnd1.Next(10, 20)
Result1 = Rnd1.Next(1, 10)
If Result0 / Result1 = Result0 \ Result1 Then
Total = Result0 / Result1
Exit Do
End If
Loop
或者,您可以生成答案和除数,然后将它们相乘得到第一个数字:)
Or, you could generate your answer and the divisor and multiply them together to give you the first number :)
Dim Result1 = Rnd1.Next(1, 10)
Dim Total = Rnd1.Next(1, 5)
Dim Result0 = Result1 * Total
这篇关于Visual Basic 数字选择器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!