1

我有一个文本框,人们可以输入一个数字或一个范围,例如 12-15,然后生成一个随机数。目前,如果第二个数字小于第一个数字,我可以让它按我想要的方式工作,但如果只列出一个数字就不行。

如果没有 words(1) 或小于 words(0),rnum1 应该等于 rnum2。(如果它更少,我确实完成了它。)

Dim words As String() = TextBox2.Text.Split("-")
Dim rnum1 As String = words(0)
Dim rnum2 As String = words(1)
Dim RandomClass As New Random()
Dim RandomNumber As Integer

If rnum2 < rnum1 Then
rnum2 = rnum1

End If

RandomNumber = RandomClass.Next(rnum1, rnum2)
4

1 回答 1

0

Change this line

Dim rnum2 As String = words(1)

to

Dim rnum2 As String = IF(words.Length = 2, words(1), rnum1)

It checks whether there're 2 elements in words array. If it is - it uses second element of array, otherwise it reassigns the first.

Come to think of it, this can be achieved even by this:

Dim rnum2 As String = words(words.Length - 1)

If there're 2 elements in the array - it will assign words(1), otherwise words(0)

于 2013-09-23T02:49:29.467 回答