Compare two numbers in a batch file

2020-05-08 07:12发布

问题:

I searched this site for my question, but I didn't find a solution to my problem.

The system gives a random number for the player and for the computer, from 2 to 12.

This has 3 parts, if X bigger than Y, if X lesser than Y, and when X is same as Y.

When I start the .bat, it works great, I choose Play Game, I enter the Bet (20 for example), but when I start this process, the window closes, and I can't read what it wrote. I can see some flashing text, I have seen 'Your' and 'syntax', but it disappears very fast.

I'm really sure it worked, because when I delete the 3 if if if things, it shows my money, bet, stats and everything, and I can even decrease or increase it with 'cheats' I wrote.

So, here it is:

set /p setbet=Please type a number to select bet: 
if "%setbet%"=="1" set bet=20
if "%setbet%"=="2" set bet=50
if "%setbet%"=="3" set bet=100
if "%setbet%"=="4" set bet=150
if "%setbet%"=="5" set bet=200
if "%setbet%"=="6" set bet=250
if "%setbet%"=="7" set bet=300
echo.
echo Your bet is %bet%.
echo.
pause
cls
set /a money-=bet
set /a playernum=%random% %%12 +2
set /a enemynum=%random% %%12 +2
echo.
echo You roll:        %playernum%
echo Enemy rolls:     %enemynum%
echo.
if %playernum% LSS %enemynum%
(
echo Enemy wins. Please try again.
echo Your current money is %money%.
echo You lost %bet% money.
echo.
pause
goto INTRO
)
if %enemynum% LSS %playernum%
(
set /a money+=bet*2
echo You win.
echo Your current money is %money%.
echo You won %bet% money.
echo.
pause
goto INTRO
)
if %enemynum% EQU %playernum%
(
set /a money+=bet
echo It's a tie.
echo You won the bet (%bet%), but your money didn't changed.
echo Your money is now %money%.
echo.
pause
goto INTRO

Thank you!

回答1:

1) You forgot to close your last parenthesis

2) The syntax error is here :

if %enemynum% EQU %playernum%
(

It should be : if %enemynum% EQU %playernum% (

3) The value of the money variable will be wrong when displayed.

It's caused by the two facts: -> Inside a FOR or a IF, the variables are "expanded" before and not during command execution. ( expanded = the variable is remplaced by its value )

In order to change the value of a variable and use it in the same loop, you should use the delayed expression.

You must write SETLOCAL ENABLEDELAYEDEXPANSION at the beginning of your code and the variable whose expansion should be delayed should be surrounded by exclamation marks instead of percent signs.

So echo Your current money is %money%. become echo Your current money is !money!.

4) You must escape your parenthesis in case of tie : echo You won the bet ^(%bet%^) but your money didn't changed.

5) You didn't put a label INTRO, so your goto INTRO will fail



回答2:

you should search for delayed expansion (setlocal enabledelayedexpansion) and count your brackets: every opening bracket needs a closing one (there are exceptions I know, but not here). The opening bracket after if must be on the same line like if. The if parser will stop here: %bet%) because this is the first closing bracket after the last opening.