【发布时间】:2015-07-19 17:24:35
【问题描述】:
我在为学校编写的程序中遇到了问题。我需要使用 Luhn 算法验证信用卡号码,但是我在让算法的逻辑正常工作方面遇到了一些困难。我相信我知道问题出在哪里,但我无法解决它。
我相信问题出在这里:
For i = 0 To cardInput.Text.Length - 2 Step -2
Dim x = (i * 2)
If x > 9 Then
x = x - 9
End If
oddTotal += x
Next
'Sum of undoubled digits
For i = 0 To cardLength - 1 Step -2
evenTotal += i
Next
total = oddTotal + evenTotal
checkSum = total
infoOutput.Items.Add("CheckDigit: " & checkDigit)
infoOutput.Items.Add("CheckSum :" & checkSum)
'Verify that the card is valid by the Mod 10 (Lund algoritm)
If checkSum = checkDigit Or checkSum = 0 Then
valid = True
Else
valid = False
End If
如果需要,我的项目的其余部分可以看到here
我的代码似乎不是从最后一个数字开始,而是每隔一个数字回到开头以加倍。 Step -2 运算符在这里不正确吗?我做错了什么?
【问题讨论】:
-
您似乎没有使用卡号的数字。
-
我不明白我是怎么做到的,但如果我使用此代码显示所选数字,它在我的列表框中显示良好。 infoOutput.Items.Add(cardInput.Text.Substring(cardLength -2,1)) 所以我的 For 循环应该选择数字并正确使用它们?
标签: vb.net