Powershell:为什么我的if语句被定向到else语句?或者,为什么评估运算符不正确?

时间:2017-06-24 22:50:53

标签: powershell if-statement operators

我有一个非常基本的PowerShell脚本,我遇到的问题是在if语句中,在函数“Cast-Wait”中。简而言之,如果你有足够的法术力来施放法术,就施放它。目前我将永远拥有1500点法力值。但是,当使用SpellCost为100评估此语句时,它将按预期工作。但是,如果SpellCost是200+,那么它会转到Else。即(1400> = 200)=假。

[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$script:mana
$script:regen
$count = 0
$continue = $true

function Get-Info {
    $script:mana = Read-Host -Prompt "Total Mana"
    $script:regen = Read-Host -Prompt "Mana Regen"
    #then we can ask about spells and priority and whatnot
}

function Time-Remaining {
    Param ([int]$SpellCost)
    # will actually be (spellcost - remainingMana) / regen
    return $SpellCost / $regen
}

function Auto-It {
    while($continue) {
        if ([console]::KeyAvailable) {
            #echo "Toggle with F12";
            $x = [System.Console]::ReadKey()

            switch ( $x.key) {
                F12 { $continue = $false }
            }
        }
        else {
            Cast-Wait -Spell 1 -SpellCost 100
            Cast-Wait -Spell 1 -SpellCost 200
            Cast-Wait -Spell 4 -SpellCost 300
            Cast-Wait -Spell 4 -SpellCost 400
            Cast-Wait -Spell 1 -SpellCost 500
            Cast-Wait -Spell 1 -SpellCost 600
            Cast-Wait -Spell 1 -SpellCost 700
            Cast-Wait -Spell 1 -SpellCost 800

            $count = $count + 1
            echo "Total count: " $count
      }
    }
}

function Cast-Wait {
    Param ([int]$Spell,[int]$SpellCost,[int]$WaitBefore,[int]$WaitAfter)

    If ($WaitBefore){Sleep -s $WaitBefore}

    If ($mana -ge $SpellCost) {
      [System.Windows.Forms.SendKeys]::SendWait($Spell)
    } Else {
      #Sleep -s (Time-Remaining -SpellCost $SpellCost)
      echo "in this else"
    }

    If ($WaitAfter){Sleep -s $WaitAfter}
}

Get-Info
Auto-It

这是if语句:

If ($mana -ge $SpellCost) {
  [System.Windows.Forms.SendKeys]::SendWait($Spell)
} Else {
  #Sleep -s (Time-Remaining -SpellCost $SpellCost)
  echo "in this else"
}

这是我尝试使用ise调试器在每次迭代中确认的不同值。

Cast-Wait -Spell 1 -SpellCost 100   #<--- this works as intended
Cast-Wait -Spell 1 -SpellCost 200   #all others do not
Cast-Wait -Spell 4 -SpellCost 300
Cast-Wait -Spell 4 -SpellCost 400
Cast-Wait -Spell 1 -SpellCost 500
Cast-Wait -Spell 1 -SpellCost 600
Cast-Wait -Spell 1 -SpellCost 700
Cast-Wait -Spell 1 -SpellCost 800

0 个答案:

没有答案