无法读取一个数字,打印输出,然后转到VB中的下一个数字

时间:2019-06-09 17:15:06

标签: vb.net

尝试创建一个用户可以输入数字的代码,然后该代码将读取第一个数字,然后说出输出。它将移至下一个号码。但是,它只会处理第一个数字,并且只会处理输入的数字。

Public Class Form1
    Private Sub btnSpeak_Click(sender As Object, e As EventArgs) Handles btnSpeak.Click
        Dim number As String = Convert.ToInt32(Char.GetNumericValue(txtNumber.Text))
        Dim x, y As Integer
        Dim numb1 As Integer = Convert.ToInt32(Len(txtNumber.Text))
        Dim numb2 As Integer = 0
        Dim test As Integer



        x = 0
        y = 30000.01

        If number < x Then
            MsgBox("error")
        Else
            If number > y Then
                MsgBox("error")
            Else

                Do Until numb2 = numb1


                    If number = 9 Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Nine.wav")
                        numb2 = numb2 + 1
                    ElseIf number = 8 Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Eight.wav")
                        numb2 += 1
                    ElseIf number = 7 Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Seven.wav")
                        numb2 += 1
                    ElseIf number = 6 Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Six.wav")
                        numb2 += 1
                    ElseIf number = 5 Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Five.wav")
                        numb2 += 1
                    ElseIf number = 4 Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Four.wav")
                        numb2 += 1
                    ElseIf number = 3 Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Three.wav")
                        numb2 += 1
                    ElseIf number = 2 Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Two.wav")
                        numb2 += 1
                    ElseIf number = 1 Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\One.wav")
                        numb2 += 1
                    ElseIf number = 0 Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Zero.wav")
                        numb2 += 1
                    ElseIf number = (".") Then
                        My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Point.wav")
                        numb2 += 1
                    End If
                    MsgBox("test")
                Loop
                MsgBox("tester")
                Close()
            End If
        End If
    End Sub
End Class

如果您输入1234,则应输入1234 但它说1111

2 个答案:

答案 0 :(得分:0)

仅将一个字符串作为参数传递的方法Char.GetNumericValue返回该字符串中第一个char的数值。如果要在字符串中 GetNumericValue 中输入第二个字符,则需要给出该字符在字符串中的位置,并且由于数组的索引从零开始,因此第二个位置是1。 / p>

当然,您需要在“直到”循环中应用此逻辑,您可以在其中更改索引以在每个循环处传递给GetNumeriCalue。

     Dim numb2 As Integer = 0
     Dim numb1 As Integer = txtNumber.Text.Length

     Do Until numb2 = numb1
        ' We want to get the value at the current loop position
        number = Convert.ToInt32(Char.GetNumeriValue(txtNumber.Text, numb2))
        If number = 9 Then
            ... the whole if elseif sequence follows with numb1 increments
        End if
     Loop

还考虑您应该添加检查以了解用户是否真的键入仅包含数字的内容,因为字符(非数字)的Char.GetNumericValue将返回-1,例如,您可以在按钮的开头添加此字符点击

   If Not Int32.TryParse(txtNumber.Text, number) Then
      MessageBox.Show("Type only numbers please!")
      Return
   End If

答案 1 :(得分:0)

一个字符串不仅是一个字符串,而且是一个Char数组。您可以在此代码中利用此功能。遍历作为字符串的Char数组中的每个Char。大小写用双引号括起来,例如字符串,后跟小写字母c,以告知编译器这是一个Char。在C#中,我们将在字符周围使用单引号,但vb在注释字符中使用单引号。 (无关紧要,只是琐事)没有转换,没有增加计数器。

Private Sub btnSpeak_Click(sender As Object, e As EventArgs) Handles btnSpeak.Click
    For Each ch As Char In TextBox1.Text
        Select Case ch
            Case "9"c
                My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Nine.wav")
            Case "8"c
                My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Eight.wav")
                'And the rest of the cases
            Case Else
                MessageBox.Show("Please enter only numeric characters")
                Exit Sub
        End Select
    Next
End Sub

另一种方法-重命名文件9.wav,8.wav,7.wav。在这段时间内将无法使用。您将需要检查“。” c和.Play Point.wav。

Private Sub ReadNumbers()
    For Each ch As Char In TextBox1.Text
        If ch = "."c Then
            My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\Point.wav")
        Else
            My.Computer.Audio.Play("F:\Computing Year 1\Nick\Unit 16 - Client Side Customisation of Webpages\In Progress\16.3\Audio clips\" & ch & ".wav")
        End If
    Next
End Sub