2015-09-25 38 views
0

(這是vb在一個控制檯應用程序btw表達)我是編程新手,所以忍受我 - 我試圖做一個小口袋妖怪的戰鬥(俗氣,我知道),除了事實上敵人在一回閤中攻擊了3次,這是一種複雜的事實,它工作得很好。這是我的代碼 - 我該如何解決這個問題?小寵物小精靈之戰 - 敵人一擊三次攻擊

Module Module1 

    Sub Main() 
     Dim num As Integer 
     Dim num1 As Integer 
     Dim num2 As Integer 
     Dim num3 As Integer 

     num = 0 
     Console.WriteLine("select the wild zigzagoon's health.") 
     num3 = Console.ReadLine 
     Console.WriteLine("select charmander's health.") 
     num2 = Console.ReadLine 
     Console.WriteLine("a wild zigzagoon appeared! go, charmander!") 

     While num3 > 0 And num2 > 0 
      Console.WriteLine("what will charmander do? use tackle(1), burn(2), smack(3) or ember(4)?") 
      num1 = Console.ReadLine 
      If num1 > 0 And num1 < 2 Then 
       num3 = num3 - 15 
       Console.WriteLine("charmander used tackle! zigzagoon took 15 damage! zigzagoon is on " & num3) 
       num = num + 1 
      ElseIf num = 1 Then 
       num2 = num2 - 10 
       Console.WriteLine("zigzagoon used tackle! charmander took 10 damage! charmander is now on " & num2) 
      ElseIf num = 2 Then 
       num2 = num2 - 20 
       Console.WriteLine("zigzagoon used quick attack! charmander took 15 damage! charmander is now on" & num2) 
      ElseIf num = 3 Then 
       num2 = num2 - 25 
       Console.WriteLine("zigzagoon used headbutt! charmander took 25 damage! charmander is now on" & num2) 
      ElseIf num = 4 Then 
       num2 = num2 - 40 
       Console.WriteLine("zigzagoon used take down! charmander took 40 damage! charmander is now on " & num2) 
      End If 

      If num1 > 1 And num1 < 3 Then 
       num3 = num3 - 20 
       Console.WriteLine("charmander used burn! zigzagoon took 20 damage! zigzagoon is now on " & num3) 
       num = num + 1 
      ElseIf num = 1 Then 
       num2 = num2 - 10 
       Console.WriteLine("zigzagoon used tackle! charmander took 10 damage! charmander is now on " & num2) 
      ElseIf num = 2 Then 
       num2 = num2 - 20 
       Console.WriteLine("zigzagoon used quick attack! charmander took 15 damage! charmander is now on" & num2) 
      ElseIf num = 3 Then 
       num2 = num2 - 25 
       Console.WriteLine("zigzagoon used headbutt! charmander took 25 damage! charmander is now on" & num2) 
      ElseIf num = 4 Then 
       num2 = num2 - 40 
       Console.WriteLine("zigzagoon used take down! charmander took 40 damage! charmander is now on " & num2) 
       num = num - 4 
      End If 

      If num1 > 2 And num1 < 4 Then 
       num3 = num3 - 30 
       Console.WriteLine("charmander used smack! zigzagoon took 30 damage! zigzagoon is now on " & num3) 
       num = num + 1 
      ElseIf num = 1 Then 
       num2 = num2 - 10 
       Console.WriteLine("zigzagoon used tackle! charmander took 10 damage! charmander is now on " & num2) 
      ElseIf num = 2 Then 
       num2 = num2 - 20 
       Console.WriteLine("zigzagoon used quick attack! charmander took 15 damage! charmander is now on" & num2) 
      ElseIf num = 3 Then 
       num2 = num2 - 25 
       Console.WriteLine("zigzagoon used headbutt! charmander took 25 damage! charmander is now on" & num2) 
      ElseIf num = 4 Then 
       num2 = num2 - 40 
       Console.WriteLine("zigzagoon used take down! charmander took 40 damage! charmander is now on " & num2) 
       num = num - 4 
      End If 

      If num1 > 3 And num1 < 5 Then 
       num3 = num3 - 40 
       Console.WriteLine("charmander used ember! zigzagoon took 30 damage! zigzagoon is now on " & num3) 
      ElseIf num = 1 Then 
       num2 = num2 - 10 
       Console.WriteLine("zigzagoon used tackle! charmander took 10 damage! charmander is now on " & num2) 
      ElseIf num = 2 Then 
       num2 = num2 - 20 
       Console.WriteLine("zigzagoon used quick attack! charmander took 15 damage! charmander is now on" & num2) 
      ElseIf num = 3 Then 
       num2 = num2 - 25 
       Console.WriteLine("zigzagoon used headbutt! charmander took 25 damage! charmander is now on" & num2) 
      ElseIf num = 4 Then 
       num2 = num2 - 40 
       Console.WriteLine("zigzagoon used take down! charmander took 40 damage! charmander is now on " & num2) 
       num = num - 4 
      End If 


     End While 

     If num3 <= 0 Then 
      Console.WriteLine("the wild zigzagoon fainted! charmander gained 30 xp!. charmander leveled up to level 6!") 
     End If 


    End Sub 

End Module 
+0

對於初學者改變你的變量爲特定的東西。例如:CharmanderCommand,CharmanderHealth,ZigazoonHealth分別爲num1,num2,num3。這將幫助您閱讀代碼。至於你提到的問題,也許而不是一個while循環,它應該是一個簡單的if語句。 –

+0

也使用Select Case語句代替elseif –

回答

0

之所以攻擊敵人3次是因爲有4 如果/結束如果塊。

啓用num在第一如果/結束如果塊(NUM = NUM​​ + 1),它就會在後續執行3次如果/結束如果塊設置。

它看起來像你的NUM如果聲明應被嵌套在第一如果語句中。有很多代碼重複(應該轉換爲函數),這使得它更難理解。

我重寫了代碼的自由:

Sub Main() 
    Dim charmanderAttack As Integer 
    Dim charmanderHealth As Integer 
    Dim zigzagonAttack As Integer 
    Dim zigzagoonHealth As Integer 

    Console.WriteLine("select the wild zigzagoon's health.") 
    zigzagoonHealth = Console.ReadLine 
    Console.WriteLine("select charmander's health.") 
    charmanderHealth = Console.ReadLine 
    Console.WriteLine("a wild zigzagoon appeared! go, charmander!") 

    zigzagonAttack = 0 
    While zigzagoonHealth > 0 And charmanderHealth > 0 
     Console.WriteLine("what will charmander do? use tackle(1), burn(2), smack(3) or ember(4)?") 
     charmanderAttack = Console.ReadLine 
     CharmanderAttacks(charmanderAttack, zigzagoonHealth) 

     zigzagonAttack += 1 
     ZigzagonAttacks(zigzagonAttack, charmanderHealth) 
    End While 

    If zigzagoonHealth <= 0 Then 
     Console.WriteLine("the wild zigzagoon fainted! charmander gained 30 xp!. charmander leveled up to level 6!") 
    End If 
End Sub 

Sub CharmanderAttacks(ByRef charmanderAttack As Integer, ByRef zigzagoonHealth As Integer) 
    Select Case charmanderAttack 
     Case 1 
      zigzagoonHealth -= 15 
      Console.WriteLine("charmander used tackle! zigzagoon took 15 damage! zigzagoon is on " & zigzagoonHealth) 
     Case 2 
      zigzagoonHealth -= 20 
      Console.WriteLine("charmander used burn! zigzagoon took 20 damage! zigzagoon is now on " & zigzagoonHealth) 
     Case 3 
      zigzagoonHealth -= 30 
      Console.WriteLine("charmander used smack! zigzagoon took 30 damage! zigzagoon is now on " & zigzagoonHealth) 
     Case 4 
      zigzagoonHealth -= 40 
      Console.WriteLine("charmander used ember! zigzagoon took 40 damage! zigzagoon is now on " & zigzagoonHealth) 
    End Select 
End Sub 

Sub ZigzagonAttacks(ByRef zigzagonAttack As Integer, ByRef charmanderHealth As Integer) 
    Select Case zigzagonAttack 
     Case 1 
      charmanderHealth -= 10 
      Console.WriteLine("zigzagoon used tackle! charmander took 10 damage! charmander is now on " & charmanderHealth) 
     Case 2 
      charmanderHealth -= 20 
      Console.WriteLine("zigzagoon used quick attack! charmander took 20 damage! charmander is now on " & charmanderHealth) 
     Case 3 
      charmanderHealth -= 25 
      Console.WriteLine("zigzagoon used headbutt! charmander took 25 damage! charmander is now on " & charmanderHealth) 
     Case 4 
      charmanderHealth -= 40 
      Console.WriteLine("zigzagoon used take down! charmander took 40 damage! charmander is now on " & charmanderHealth) 
      zigzagonAttack -= 4 
    End Select 
End Sub 
+0

啊,我明白了!感謝您的解釋!我不知道它是這樣工作 - 謝謝:D – channoz

+0

沒問題。這將是偉大的,如果你能投票我的回答:) – codersl