我正在使用第7个字符的校验位。程序以有效输入运行。当我故意使用错误的校验位时,我的if语句不会执行?无法弄清楚为什么我在后续的其他功能运行时不会执行。
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
Dim Sel As Integer, test As Boolean = False
Dim PointerA As Integer
txtResult.Clear()
While test <> True And Sel < StudentNameArray.Length
If radName.Checked = True Then
If txtInput.Text = StudentNameArray(Sel) Then
PointerA = StudentNameArray.Length
txtResult.Text = "The ID is: " & StudentIDArray(Sel)
test = True
End If
End If
If radID.Checked = True Then
If txtInput.Text = StudentIDArray(Sel) Then
Dim idNum As Integer
Dim pos1 As Integer
Dim pos2 As Integer
Dim pos3 As Integer
Dim pos4 As Integer
Dim pos5 As Integer
Dim pos6 As Integer
Dim pos7 As Integer
Dim subTot As Integer = 0
Dim m1 As Integer
idNum = txtInput.Text
pos1 = (Mid(idNum, 1, 1))
pos2 = (Mid(idNum, 2, 1))
pos3 = (Mid(idNum, 3, 1))
pos4 = (Mid(idNum, 4, 1))
pos5 = (Mid(idNum, 5, 1))
pos6 = (Mid(idNum, 6, 1))
pos6 = pos6 * 2
pos5 = pos5 * 3
pos4 = pos4 * 4
pos3 = pos3 * 5
pos2 = pos2 * 6
pos1 = pos1 * 7
subTot = (pos1 + pos2 + pos3 + pos4 + pos5 + pos5)
m1 = subTot Mod 11
m1 = 11 - m1
pos7 = (Mid(idNum, 7, 1))
If pos7 <> m1 Then
txtResult.Text = "Invalid ID, Select Clear or Exit"
Else
PointerA = StudentIDArray.Length
txtResult.Text = "The Name is: " & StudentNameArray(Sel)
test = True
End If
End If
End If
Sel = Sel + 1
End While
End Sub
答案 0 :(得分:1)
您似乎没有得到校验和计算权。
首先,以下一行:
subTot = (pos1 + pos2 + pos3 + pos4 + pos5 + pos5)
应该是:
subTot = (pos1 + pos2 + pos3 + pos4 + pos5 + pos6)
请注意,您有pos5
两次而没有pos6
。
并且,要使用mod计算校验和,它应该看起来像这样从0
到9
得到一个数字:
m1 = subTot Mod 10
m1 = 9 - m1
您现有的代码可以生成从1
到11
而不是0
到9
的值。
在任何情况下,请尝试使用此代码计算值:
Dim computed = _
txtInput.Text _
.ToCharArray() _
.Select(Function(c, n) (txtInput.Text.Length - n) * (c - "0"c)) _
.ToArray()
Dim m1 = 9 - (computed.Take(txtInput.Text.Length - 1).Sum() Mod 10)
Dim pos7 = computed.Last()
此代码具有额外的优势,可以使用任何输入数字。