(这是vb express在控制台应用程序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 :(得分:0)
敌人攻击3次的原因是因为有4个 If / End If 阻挡。
当在第一个 If / End If 块( num = num + 1 )中设置num时,它会在后续的 If /中执行3次结束如果阻止。
您的 num If 语句应该嵌套在第一个 If 语句中。有很多代码重复(应该转换为函数),这使得它更难理解。
我冒昧地重写你的代码:
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