vb.net - 无法读取一个数字,打印输出,然后在 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
解决方案
仅将字符串作为参数传递的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
推荐阅读
- php - Codeigniter 4 验证总是使用 getJSON 返回 false
- git - Git推送速度波动和缓慢-如何加快?
- angular - Angular Observable 组件和服务交互
- python - 将 pandas 数据框值与字典键进行比较,然后将该字典中的值插入到新列中
- actions-on-google - Google Actions 未在履行请求中传递用户信息(访问令牌/身份令牌)
- python - 合并 2 个数据帧,键分为 2 列
- powerbi - 在 DAX Power BI 中满足条件后自动递减/递增排名
- r - 我可以使用 fread 下载文件夹中的所有文件吗?
- mysql - 在docker中运行wordpress mysql数据库时如何访问它?
- html - 如何使用 viewBox 属性将 SVG 正确嵌入到 img 元素中?