首页 > 解决方案 > 无法读取一个数字,打印输出,然后在 VB 中移动到下一个数字

问题描述

我试图制作一个用户可以输入数字的代码,然后代码将读取第一个数字,然后说出输出。它将移动到下一个数字。但是它只会输入第一个数字,并且会输入多少个数字。

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

标签: vb.net

解决方案


仅将字符串作为参数传递的Char.GetNumericValue方法返回该字符串中第一个字符的数值。如果要获取字符串中第二个字符的GetNumericValue,则需要给出该字符在字符串中的位置,并且因为数组的索引从零值开始,所以第二个位置是 1。

当然,您需要在 Do Until 循环中应用此逻辑,您可以在每个循环中更改要在 GetNumeriCalu 处传递的索引。

     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

推荐阅读