0

上次我们有活动。它正在将我同学的 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/enter 的其他用途……我不太了解他。您认为我的代码有什么问题?它正在运行,但他说我的代码是错误的。

4

2 回答 2

2

首先,清理一下你的代码。

  • 与其使用所有这些 Else If 语句,更好的方法是使用 Select Case。
  • 您不必在第 3 行或第 4 行使用 Int()。它已经是一个整数。
  • 您也不必重复“您是十二生肖”的字符串。只需使用一次。
  • 正如 Deanna 所说,您需要从文本框输入中删除 ASCII 13。

所以,新代码:

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

现在,我看到了其他几个问题。

  • 你有硬编码的年份。十二生肖与您出生的年份有关,与当前年份没有任何关系。
  • 您有 0 列出了两次。

现在,这对你来说可能太高级了,但这是我的编码方式:

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 函数,我得到了数组的正确索引。

于 2013-06-21T15:35:54.547 回答
2

而且,使用起来更合适

vbKeyReturn

而不是数字 13,因为并非所有的键盘 ENTER 都是 13。

所以正确的方法是:

If KeyAscii = vbKeyReturn Then

(请评价我的回答,谢谢!)

于 2013-06-24T22:12:35.520 回答