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

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

问题描述

我在这个网站上搜索我的问题,但我没有找到解决我的问题的办法。



系统给玩家一个随机数字,计算机从2到12。



如果X大于Y,如果X小于Y,且X与Y相同,则有3个部分。 >

当我开始 .bat 时,效果非常好,我选择玩游戏,我进入赌注(例如20),但是当我开始这个过程,窗口关闭,我不能读它写的。我可以看到一些闪烁的文本,我看到了'你的'和'语法',但它消失得非常快。



我真的确定它的工作,删除3 如果如果的东西,它显示我的钱,赌注,统计和一切,我甚至可以减少或增加它与我写的骗子。



所以,这里是:

  set / p setbet =数字选择下注:
如果%setbet%==1set bet = 20
如果%setbet%==2set bet = 50
如果%setbet% setbet%==3set bet = 100
如果%setbet%==4set bet = 150
如果%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您当前的钱是%money%。成为 echo您当前的钱是!money! / code>



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



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


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) 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天全站免登陆