简体   繁体   中英

Find the biggest of 4 numbers in Windows Batch scripting

I am trying to find the biggest of 4 numbers in batch scripting but it is not working.

The GTR command is not getting executed.

From this line it never gets executed if !Counter_Senior! gtr !Max_Age! ( if !Counter_Senior! gtr !Max_Age! (

I'm new to batch scripting, I am not sure about alignment and spacing. please help me through.

echo off
setlocal enabledelayedexpansion

set /a Counter_Child=1
set /a Counter_Senior=2 
set /a Counter_Older_adult=0
set /a Counter_Young_adult=3
set /a Max_Age=%counter_Child%

echo maximum age is %Max_Age%

if !Counter_Senior! gtr !Max_Age! (
    set Max_Age=%Counter_Senior%
    if !Counter_Older_adult! gtr !Max_Age! (
        set Max_Age=%Counter_Older_adult%
        if !%Counter_Young_adult! gtr !Max_Age! (
            set Max_Age=%Counter_Young_adult%
            time /t
            echo Maximum age is %Max_Age%
            goto:EOF
        )
    )
)

if !Counter_Older_adult! gtr !Max_Age! (
    set %Max_Age%=%Counter_Older_adult%
    if !%Counter_Young_adult! gtr !Max_Age! (
        set %Max_Age%=%Counter_Young_adult%
        echo Maximum age is %Max_Age% 
        goto:EOF
    )
) 

if !%Counter_Young_adult! gtr !Max_Age! (
    set %Max_Age%=%Counter_Young_adult%
    echo Maximum age is %Max_Age% 
    goto:EOF
)   

echo Maximum age is %Max_Age%

goto:EOF

Your code seems way too complicated. Try this:

echo off
set Counter_Child=1
set Counter_Senior=2 
set Counter_Older_adult=0
set Counter_Young_adult=3
set Max_Age=%Counter_Child%
IF %Counter_Senior% GTR %Max_Age% SET Max_Age=%Counter_Senior%
IF %Counter_Older_adult% GTR %Max_Age% SET Max_Age=%Counter_Older_adult%
IF %Counter_Young_adult% GTR %Max_Age% SET Max_Age=%Counter_Young_adult%
echo maximum age is %Max_Age%

If you only want the maximum value of the four Counter variables then you could use this:

For /F "Tokens=2 Delims==" %%A In ('Set Counter_'
) Do If %%A GEq !Max_Age! Set/A Max_Age=%%A

[Edit]
And for the scenario suggested by Anders

For %%A In (
    %Counter_Child% %Counter_Senior% %Counter_Older_adult% %Counter_Young_adult%
) Do If %%A GEq !Max_Age! Set/A Max_Age=%%A

There are (at least) two problems with your code:

  • You are using goto:EOF inside a if and that quits the entire batch file.
  • In some places you incorrectly used set %Max_Age%=.. instead of set Max_Age=..

Your code is also overcomplicated and can be reduced to something simpler like the answer posted by MichaelS .

If you want to print specific information when a change is made you can use a generic sub procedure:

@echo off
goto Start

:CheckAge 
setlocal enableextensions enabledelayedexpansion
set newage=!%1!
if %newage% gtr %Max_Age% (
    echo %1 is older than %Max_Age%, the new maximum is %newage%
    set Max_Age=%newage%
)
endlocal & set Max_Age=%Max_Age%
goto :EOF

:Start
set Counter_Child=1
set Counter_Senior=2
set Counter_Older_adult=0
set Counter_Young_adult=3
set Max_Age=0

call :CheckAge Counter_Child
call :CheckAge Counter_Senior
call :CheckAge Counter_Older_adult
call :CheckAge Counter_Young_adult
echo Maximum age is %Max_Age%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM