我需要使用数组和字符串。该程序应该只搜索用户将输入的1个号码(0-9)。这是我的代码如下。它使用一个数组来获取一个随机数,并且它正在测试每个数字的出现次数。但是它没有用,但我不需要它来测试每个数字。
程序必须只测试用户请求的1号码。因此,如果生成的随机数是...'7417',并且用户用户选择了'7',那么程序将报告已经有两个七位数。我将使用文本框'txtEnter'来获取要搜索的用户号码。谁能帮我?谢谢!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Randomize()
Dim ArrayNum(1) As Integer
ArrayNum(0) = Int(Rnd() * 100000)
lblDisplayNumber.Text = ArrayNum(0)
Dim num As Integer
txtEnter.Text = num
Dim counts = From c In num.ToString()
Group By c Into Group
Select DigitGroup = New With {.Count = Group.Count(),
.Digit = c, .Group = Group}
Order By DigitGroup.Count Descending
Select String.Format("There are {0} number {1}'s found.",
DigitGroup.Count, DigitGroup.Digit)
Dim message = String.Join(Environment.NewLine, counts)
End Sub
答案 0 :(得分:2)
如果您想使用Linq,它简短易读:
Dim counts = From c In num.ToString()
Group By c Into Group
Select DigitGroup = New With {.Count = Group.Count(), .Digit = c, .Group = Group }
Order By DigitGroup.Count Descending
Select String.Format("There are {0} number {1}'s found.",
DigitGroup.Count, DigitGroup.Digit)
Dim message = String.Join(Environment.NewLine, counts)
更新:这是一个完整的示例,显示输入的数字和摘要的结果:
Dim rnd As New Random()
Dim randomInt = rnd.Next(0, 100000)
lblDisplayNumber.Text = randomInt.ToString()
Dim num As Integer
If Int32.TryParse(txtEnter.Text, num) AndAlso num >= 0 AndAlso num < 10 Then
Dim counts = From c In randomInt.ToString()
Group By c Into Group
Select DigitGroup = New With {.Count = Group.Count(), .Digit = c, .Group = Group}
Order By DigitGroup.Count Descending, DigitGroup.Digit
Dim numCount = counts.FirstOrDefault(Function(grp) grp.Digit.ToString() = num.ToString())
If numCount IsNot Nothing Then
Dim numMessage = String.Format("There are {0} number {1}'s found.", numCount.Count, num)
MessageBox.Show(numMessage)
Else
MessageBox.Show("Number does not contain " & num)
End If
Dim summaryCount = From grp In counts
Select String.Format("There are {0} number {1}'s found.", grp.Count, grp.Digit)
Dim summaryMessage = String.Join(Environment.NewLine, summaryCount)
MessageBox.Show("Summary:" & Environment.NewLine & summaryMessage)
Else
MessageBox.Show("Please enter a number between 0 and 9.")
End If
答案 1 :(得分:1)
您的代码有几个问题,但要直接回答缺少的问题:
您应该将随机数转换为字符串
Number= Int(Rnd() * 100000)
NumberString = Number.ToString()
将该字符串转换为字符数组
DigitArray = NumberString.ToCharArray()
然后循环遍历数组的所有字符,并将每个字符c
转换回整数
For Each c As Char In DigitArray
num = c - "0"C`
然后使用num
作为counts
数组的索引
count(num)+=1
而不是那种可怕的switch
陈述。我让你弄清楚这些变量的正确Dim
语句。
答案 2 :(得分:1)
如果您只需要查找输入的数字在随机数字中的次数,前面介绍的解决方案似乎有点复杂。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' create the data
Dim nDigits As Integer = 5
Dim digits(nDigits - 1) As String
For i = 0 To nDigits - 1
digits(i) = CInt(Rnd() * 10).ToString
Next
' show the data
TextBox1.Text = String.Join("", digits)
Dim inp = InputBox("Enter a number from 0 to 9")
' count the occurrences of the entered number
Dim nFound = digits.Count(Function(d) d = inp)
MsgBox(String.Format("There are {0} occurrences of {1}.", nFound, inp))
End Sub