我们上次有一项活动。它将我同学的python代码转换为vb ...这是我的最终代码,它正在运行。
Private Sub txtInput_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
curyear = Int(2013)
a = Int((curyear - txtInput.Text) Mod 12)
txtInput.Text = " "
If (a = 9) Or (a = -3) Then
txtOutput.Text = "Your zodiac sign is Snake"
ElseIf (a = 8) Or (a = -4) Then
txtOutput.Text = "Your zodiac sign is Dragon"
ElseIf (a = 7) Or (a = -5) Then
txtOutput.Text = "Your zodiac sign is Rabbit"
ElseIf (a = 6) Or (a = -6) Then
txtOutput.Text = "Your zodiac sign is Tiger"
ElseIf (a = 5) Or (a = -7) Then
txtOutput.Text = "Your zodiac sign is Ox"
ElseIf (a = 4) Or (a = -8) Then
txtOutput.Text = "Your zodiac sign is Rat"
ElseIf (a = 3) Or (a = -9) Then
txtOutput.Text = "Your zodiac sign is Pig"
ElseIf (a = 2) Or (a = -10) Then
txtOutput.Text = "Your zodiac sign is Dog"
ElseIf (a = 1) Or (a = -11) Then
txtOutput.Text = "Your zodiac sign is Rooster"
ElseIf (a = 0) Or (a = -2) Then
txtOutput.Text = "Your zodiac sign is Monkey"
ElseIf (a = 11) Or (a = -1) Then
txtOutput.Text = "Your zodiac sign is Sheep"
ElseIf (a = 12) Or (a = 0) Then
txtOutput.Text = "Your zodiac sign is Horse"
End If
End If
End Sub
我们的教授告诉我其他使用ascii 13 /进入......我无法理解他。您认为我的代码有什么问题?它正在运行,但他说我的代码错了。
答案 0 :(得分:2)
首先,稍微清理你的代码。
所以,新代码:
Private Sub txtInput_KeyPress(KeyAscii As Integer)
Dim ZodiacAnimal As String
If KeyAscii = 13 Then
curyear = 2013
a = (curyear - txtInput.Text) Mod 12
Select Case a
Case 9, -3
ZodiacAnimal = "Snake"
Case 8, -4
ZodiacAnimal = "Dragon"
Case 7, -5
ZodiacAnimal = "Rabbit"
Case 6, -6
ZodiacAnimal = "Tiger"
Case 5, -7
ZodiacAnimal = "Ox"
Case 4, -8
ZodiacAnimal = "Rat"
Case 3, -9
ZodiacAnimal = "Pig"
Case 2, -10
ZodiacAnimal = "Dog"
Case 1, -11
ZodiacAnimal = "Rooster"
Case 0, -2
ZodiacAnimal = "Monkey"
Case 11, -1
ZodiacAnimal = "Sheep"
Case 12, 0
ZodiacAnimal = "Horse"
End Select
txtInput.Text = "Your zodiac sign is " & ZodiacAnimal
KeyAscii to 0
End If
End Sub
现在,我看到了其他一些问题。
现在,这对你来说可能太先进了,但这就是我对它的编码方式:
Private Sub txtInput_KeyPress(KeyAscii As Integer)
Dim ZodiacAnimal() As String
If KeyAscii = 13 Then
ZodiacAnimal = Split("Monkey,Rooster,Dog,Pig,Rat,Ox,Tiger,Rabbit,Dragon,Snake,Horse,Goat", ",")
txtInput.Text = "Your zodiac sign is " & ZodiacAnimal(Y Mod 12)
KeyAscii to 0
End If
End Sub
这使得ZodiacAnimal成为动物的一个阵列。然后使用Mod函数,我得到了数组的正确索引。
答案 1 :(得分:2)
另外,使用
的方式更为合适vbKeyReturn
而不是数字13,因为并非所有键盘都是ENTER。
所以正确的方法是:
If KeyAscii = vbKeyReturn Then
(请评价我的回答,谢谢!)