【问题标题】:Can you use two variables for GEQ comparisons in batch您可以批量使用两个变量进行 GEQ 比较吗
【发布时间】:2020-04-06 11:58:54
【问题描述】:

目前我正在尝试制作一款由粉丝制作的口袋妖怪游戏。我正在做一个卖精灵球的人。这是我用来制作它的代码。但由于某种原因,该文件无法执行最后一部分。顺便说一句,我也是编码的菜鸟。至少在某种程度上。另外,我使用Windows Batch。

@echo off
:setvariables
cls
set/a pokeballs=0
set/a pokemondollar=1000
set/a manprice=500

:Pokeball_Sale
cls
echo How many do you want to buy?
set/p pokeballamount=
set/a totalpokeballprice="pokeballamount * manprice"
echo The price = %totalpokeballprice%
pause 
echo Do you want to buy it?
echo Press y for yes
echo Or press n for no
set/p ha=Choose

if %ha% == y goto Payment
if %ha% == n goto Upstairs_House3

:Payment
cls
if "pokemondollar%" GEQ "pokeballprice" set/a pokemondollar-=totalpokeballprice
set/a pokeballs+=pokeballamount
echo You spent %totalpokeballprice% on %pokeballamount%. You now have %pokeballs%.

【问题讨论】:

  • 虽然set /a 不需要变量周围的百分号,但if 需要:if "pokemondollar%" GEQ "pokeballprice" 应该是if "%pokemondollar%" GEQ "%pokeballprice%"
  • 考虑使用choice 而不是set /p 进行简单的选择(例如Y/N) - 它自己进行错误处理(您只能输入有效的选择,所有其他输入都被拒绝)
  • 顺便说一句,如果你想尝试和学习编码,我建议你放弃 windows 批处理,转而使用更现代和友好的脚本语言(即 python)
  • @FedericoDestefanis,python 几乎不是现代的,它已经快 30 年了!它也不是windows 操作系统的一部分。

标签: windows batch-file variables cmd equals


【解决方案1】:

除了您在 cmets 中已经给出的建议之外,我决定发布这个示例,展示一种更有意义的方法。

@Echo Off

:SetVariables
ClS
Set /A PokeBalls=0,PokemonDollar=1000,ManPrice=500

:PokeBall_Sale
ClS
Set /A PokeBallAmount=0,MaxPokeBalls=PokemonDollar/ManPrice
Set /P "PokeBallAmount=How many Poke Balls do you want to buy [Maximum %MaxPokeBalls%]? "
If %PokeBallAmount% Equ 0 GoTo Upstairs_House3
If %PokeBallAmount% Gtr %MaxPokeBalls% (
    Echo You do not have enough funds!
    "%__AppDir__%timeout.exe" 3 /NoBreak>NUL
    GoTo PokeBall_Sale
)
Set /A TotalPokeBallPrice=PokeBallAmount*ManPrice
"%__AppDir__%choice.exe" /M "The cost is %TotalPokeBallPrice%. Do you want to buy it"
If ErrorLevel 2 GoTo Upstairs_House3

:Payment
ClS
If %PokemonDollar% GEq %TotalPokeBallPrice% (
    Echo You spent %TotalPokeBallPrice% on %PokeBallAmount% Poke Balls.
    Set /A PokemonDollar-=TotalPokeBallPrice
    Set /A PokeBalls+=PokeBallAmount
    SetLocal EnableDelayedExpansion
    Echo You now have !PokeBalls! Poke Balls and !PokemonDollar! Pokemon Dollars.
    EndLocal
    "%__AppDir__%timeout.exe" 5 /NoBreak>NUL

)

:Upstairs_House3
ClS

【讨论】:

    【解决方案2】:

    是的,两个整数变量确实可以在比较中使用,只要它们被扩展。为了比较而引用整数是一种防止无效变量的方法,另一种是利用延迟扩展(启用)

    There's a great explanation of integer comparison syntax issues here

    如果您有兴趣,可以使用下面的 MathCro 来分配和修改变量。如果尝试使用未声明的变量对第一个参数进行操作,则不会更改变量。

    @Echo Off
    
    %= Establish Macros =%
        setlocal DisableDelayedExpansion
        (set LF=^
    
    
        %= Newline =%)
        Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
    
        (Set "Operate=Endlocal ^& Set /A ""%%G%%H=%%I"""
    
    %= 'Tunnels' variable value, Allows Definition of Arithmetic within Macro =%)
    
        Set @M=for /L %%n in (1 1 2) do if %%n==2 (%\n%
        for /F "tokens=1,2,3 delims=, " %%G in ("!argv!") do (%\n%
        %Operate%%\n%
    %=  Display value of variable. Optional. If not syntax is required to constrain expansion =%
        If Not "!%%G!"=="" Echo(%%G: !%%G!%\n%
        ) %\n%
    ) ELSE setlocal enableDelayedExpansion ^& set argv=, 
    
    
    %= script main body =%
    %=  Facilitate modification of variables within codeblocks. =%
    Setlocal EnableDelayedExpansion
    
    REM macro can be used to define as well as modify variables
    REM output of macro can be redirected to nul (hidden) like so:
    REM (%@M% hp + 50)>nul
    
        %@M% hp + 50
        (%@M% heal + 40)>nul
        %@M% hp - 30
        %@M% hp + 25
        %@M% hp * 3
        %@M% hp / 2
        %@M% hp + heal
        %@M% heal - 10
        %@M% hp + heal
    %=  Demonstrates use of an equation beyond the initial Operator. Spaces and parentheses in equation must be ommited =%
    %=  Increments variable by a random amount in the range of 10 to 20 =%
        For /L %%A in (1,1,50) do (%@M% hp + !random!%%10+10)>nul
        Echo(hp: %hp%
        For /L %%A in (1,1,50) do IF Not !hp! LSS !heal! (%@M% hp - !random!%%15+10)
    
    pause >nul
    Exit /B
    

    【讨论】:

      猜你喜欢
      • 2013-05-03
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      相关资源
      最近更新 更多