2013-06-21 29 views
0

上次我們有一個活動。它將我的同學的python代碼轉換爲vb ...這是我的最終代碼,它正在運行。我的程序有什麼問題?關於Ascii 13 [輸入]

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

我唯一能想到的是,除非你將'KeyAscii'設置爲0,否則它將由文本框處理,這取決於'Multiline'屬性將被解釋爲一個新行。我會要求他們澄清。 – Deanna

+0

你已經硬編碼當前年份。也許這是一個問題? – MPelletier

回答

2

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

  • 而不是使用所有其他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函數,我得到數組的正確索引。

+0

非常感謝湯姆! – Harriet

2

也,它的方式更propper使用

vbKeyReturn

,而不是13號的,因爲不是所有的鍵盤上的回車是13

所以有道將是:

If KeyAscii = vbKeyReturn Then 

(請評價我的答案,臨)