2

我需要使用数组和字符串。该程序应该只搜索用户将输入的 1 个数字 (0-9)。下面是我的代码。它使用一个数组来获取一个随机数,并测试每个数字的出现次数。但是它不起作用,但无论如何我不需要它来测试每个数字。

程序必须只测试用户请求的数字 1。因此,如果生成的随机数是“7417”,并且用户用户选择了“7”,那么程序将报告有两个 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
4

3 回答 3

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
于 2013-03-24T18:26:05.363 回答
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这些变量的正确陈述。

于 2013-03-24T18:16:38.070 回答
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
于 2013-03-24T19:55:13.333 回答