gpt4 book ai didi

vba - If/Else GoTo VBA Excel

转载 作者:行者123 更新时间:2023-12-04 21:05:44 25 4
gpt4 key购买 nike

VBA 非常新。尝试搜索无济于事。我有以下代码:

monsterRollingForHit: rollForMonsterHit = (Int(2 * Rnd))
MsgBox rollForMonsterHit, 0, "Monster Hit Roll"
If rollForMonsterHit = 1 Then
GoTo monsterRollingForDmg
Else
GoTo playerRollingForHit
End If

'if monster hits we then roll for his base damage
'using a working around for randBetween due to Analysis Toolpak being required for that function
monsterRollingForDmg: rollForMonsterDmg = ((Int((6 * Rnd) + 1)))
MsgBox rollForMonsterDmg, 0, "Monster Dmg Roll"
GoTo monsterRollingForCrit

'we then add crit if the monster critically hits
monsterRollingForCrit: rollForMonsterCrit = (rollForMonsterDmg + ((Int(2 * Rnd)) * 8))
MsgBox rollForMonsterCrit, 0, "Monster Crit Roll"
GoTo rollingForPlayerArmor

'finally we reduce the monster's dmg with potential crit by the player's armor
rollingForPlayerArmor: finalMonsterDmg = (rollForMonsterCrit * (((Int((26 * Rnd) + 75))) / 100))
MsgBox finalMonsterDmg, 0, "Monster Final Dmg"
GoTo reducePlayerHealth

reducePlayerHealth: currentPlayerHP = (currentPlayerHP - finalMonsterDmg)
MsgBox currentPlayerHP, 0, "Current Player HP"
If currentPlayerHP > 0 Then
GoTo playerRollingForHit
Else
MsgBox "Monster Wins"
Exit Sub
End If

问题是即使 rollForMonsterHit 值为 0,它也永远不会进入 playerRollingForHit。相反,它只是滚动直到它得到一个 1 然后继续。

问题:需要跳过 Else 条件的代码体

最佳答案

使用 Option Explicit帮助查找变量名中的拼写错误。

这将是您似乎正在尝试的伪代码。另请注意,没有 goto 或标签。

Set MonsterHP and HeroHP
while monsterHP>0 and HeroHP>0
if MonsterHit then
work out monsterdmg
decide if critical
deduct from heroHP, accounting for any armor/dodge/etc
endif
if HeroHit then
work out HeroDmg
decide if critical
deduct from MonsterHP, accounting for any armor/dodge/etc
endif
wend
if monsterdmg>0 then
print "Monster Wins"
elseif heroHP>0 then
print "Hero wins"
else
print "They killed each other with their final blow!"
endif

关于vba - If/Else GoTo VBA Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20957187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com