批处理脚本找不到最低值

时间:2017-11-24 13:57:52

标签: batch-file

当我输入内容时,它只输出最后一个值而不是最低值

echo.
echo enter number
set temp=9999
set /p pirm=

        if "%pirm%" equ "" goto error

(for %%a in (%pirm%) do (

    if %temp% gtr %%a (
    set/a temp=%%a
    echo temp = %temp% , a = %%a
    pause>nul
    ) else (
    echo else
    )
))
echo Lowest number
echo %temp%

goto reset2

INPUT:

1 5 7 9 3 25 15

输出:

enter number
1 5 7 9 3 25 15
temp = 100 , a = 1
temp = 100 , a = 5
temp = 100 , a = 7
temp = 100 , a = 9
temp = 100 , a = 3
temp = 100 , a = 25
temp = 100 , a = 15
Lowest number
15

我对批处理脚本很新,所以我可能会犯一个简单的语法错误

基本上我想做的是将输入作为数组,然后检查数字是否低于当前最小值,如果它是新的最小值

1 个答案:

答案 0 :(得分:0)

尝试使用delayed expansion

setlocal enableDelayedExpansion
echo.
echo enter number
set temp=9999
set /p pirm=

if "%pirm%" equ "" goto error

(for %%a in (%pirm%) do (

    if !temp! gtr %%a (
      set/a temp=%%a
      echo temp = !temp! , a = %%a
      pause>nul
    ) else (
      echo else
    )
))
echo Lowest number
echo %temp%