2013-03-24 48 views
2

我需要使用数组和字符串。该程序应该只搜索用户将输入的一个数字(0-9)。以下是我的代码如下。它使用一个数组来获得一个随机数,并且它正在测试每个数字的出现次数。然而,它不起作用,但我不需要它来测试每个数字。Visual Basic - 计算随机数组中数字的出现

程序只能测试用户请求的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 

回答

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 
+0

谢谢,我试图替换你直接在case语句下面发布的代码(所以我摆脱了下面的代码的其余部分)。我得到这个错误“局部变量'计数'已经在当前块中声明。”我试图摆脱其他计数声明,但它只是创造了更多的错误。你能帮我吗?这是我应该做的吗? – 2013-03-24 18:39:11

+0

@BillStack:“在案例陈述之下”?你可以用它替换整个代码;-)你得到了编译器错误,因为你已经声明了一个名字相同的变量:'Dim counts(9)As Integer'。如果你想使用我的代码,你必须至少使用.NET 3.5和'Imports System.Linq'。我的变量'num'是用户输入的整数之一。 – 2013-03-24 18:47:17

+0

UGH!我用你的代码编辑了我的代码,我不是在做什么?我希望我能理解编程,很抱歉占用你的时间! – 2013-03-24 18:56:57

1

你的代码中有几个问题,而是给你一个答案直接到缺什么:

您应该将随机数转换为字符串第一

Number= Int(Rnd() * 100000) 
NumberString = Number.ToString() 

该字符串转换为一组字符

DigitArray = NumberString.ToCharArray() 

然后遍历数组的所有字符和conv ERT每个字符c返回一个整数

For Each c As Char In DigitArray 
    num = c - "0"C` 

然后使用num为索引您counts阵列

 count(num)+=1  

,而不是那个可怕的switch声明。我让你找出这些变量的正确Dim声明。

+0

感谢您的回复,但哇我的头旋转。我是一个新手程序员,并不能真正遵循所有这些。我知道这很可悲,但我一直在坚持这个代码不工作,并不能解决问题 – 2013-03-24 18:29:32

+0

@BillStack:看到我的编辑,我试图让我的简单解释更简单,如果你不明白它知道,我帮不了你。 – 2013-03-24 18:36:51

+0

我试着玩,但..没有拿出任何东西。不过谢谢你。我是一个失败的原因 – 2013-03-24 19:02:30

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