比较批处理文件中的两个数字 [英] Compare two numbers in a batch file

查看:103
本文介绍了比较批处理文件中的两个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜寻这个网站的问题,但我没有找到我的问题在哪里。 (对不起,我的英语不好)



系统给玩家和电脑的一个随机数字,从2到12.
这有3个部分,如果X大于Y,如果X小于Y,当X与Y相同时。
当我开始.bat,它的工作伟大,我选择玩游戏,我进入赌注,(20例如)
,但是当我开始这个过程,窗口关闭,我不能读取它写了什么(因为我可以看到一些闪烁的文本,我看到'你的'和'语法,但它消失得非常快。 / p>

我真的确定它的工作,因为当我删除3如果如果事情,它显示我的钱,赌注,统计和一切,我甚至可以减少或增加



所以,这里是:

  set / p setbet =请输入一个号码以选择下注:
如果%setbet%==1set bet = 20
如果%setbet%==2set如果%setbet%==4set bet = 150
如果%setbet%==3set bet = 100
如果%setbet% ==5set bet = 200
if%setbet%==6set bet = 250
if%setbet%==7set bet = 300
echo。
echo您的赌注是%bet%。
echo。
pause
cls
set / a money- = bet
set / a playernum =%random%%% 12 +2
set / a enemynum =%random% %% 12 +2
echo。
echo你滚动:%playernum%
echo敌人卷轴:%enemynum%
echo。
如果%playernum%LSS%enemynum%

)echo Enemy胜利请重试
echo您当前的钱是%money%。
echo您输了% bet%money。
echo。
pause
goto INTRO

如果%enemynum%LSS%playernum%

set / a money + = bet * 2
echo你赢了
echo你当前的钱是%money%。
echo你赢了%bet%钱
echo。
pause
goto INTRO

如果%enemynum%EQU%playernum%

set / a money + = bet
echo这是一条领带
echo你赢了下注(%bet%),但你的钱没有改变。
echo你的钱现在是%money%。
echo。
pause
goto INTRO

谢谢!

解决方案

1)您忘记关闭您最后一个括号



2)语法错误如下:

 如果%enemynum%EQU%playernum%


$ b b

应该是:如果%enemynum%EQU%playernum%(



3)



这是由两个事实引起的:
- >在FOR或IF中,变量被展开之前和不在命令执行期间。 (expanded =变量被其值重新放置)



为了更改变量的值并在同一个循环中使用它,您应该使用延迟表达式。



您必须在代码开头处写入SETLOCAL ENABLEDELAYEDEXPANSION,而其扩展应该被延迟的变量应该用惊叹号而不是百分号包围。



所以 echo您当前的钱是%钱%。成为 echo您当前的钱是!money! / code>



4)在绑定的情况下你必须转义你的括号:
echo你赢了押注^



5)您没有放置标签INTRO,因此您的 goto INTRO 将失败


I searched this site for my question, but i didn't find where is my problem. ( Sorry for my bad english )

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, 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 cant read what did it wrote ( because i can see some flashing text, i have seen 'Your' and 'syntax', but it disappears very fast.

Im 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 is it:

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) 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

这篇关于比较批处理文件中的两个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆