%random%isn不能正常工作

时间:2015-07-13 07:08:06

标签: windows batch-file random

我制作一个.bat文件,为二十一点游戏编码创建一个帐户。在游戏中我遇到代码%random%的问题,我试图让它在数字21和4之间选择一个随机数。这是完整的代码。我遇到问题的行显示为***** [CODE] *****

    @Echo off
    Color 02
    title Account Creation
    mode con cols=44 lines=6

    Echo Enter to continue
    pause>nul

    :newORexisting
    cls
    Echo     New account? [1]
    Echo Existing account [2]
    set /p answer=
    if %answer%==1 goto new
    if %answer%==2 goto load

    :WI
    cls
    Echo Sorry. Wrong input.
    Echo Enter to retry.
    pause>nul
    goto newORexisting

    :new
    cls
    Echo Type your desired account name...
    Echo then hit enter.
    Echo.
    Echo ~*~ NO SPACES ~*~
    set /p acctname=
    goto Next

    :next
    cls
    Echo Now type your
    Echo desired password.
    Echo.
    Echo ~*~ NO SPACES ~*~
    set /p password=
    cls
    Echo Confirm your password is...
    Echo %password%
    Echo is this correct? [y] [n]
    set /p confirmation=
    if %confirmation%==y goto createACCT
    if %confirmation%==n goto Retry

    :retry
    Echo Sorry.
    Echo Press enter to re-enter your password.
    pause>nul
    goto next

    :createACCT
    color 02
    cls
    Echo Creating account,
    Echo %acctname%
    Echo Please Wait!
    ping localhost -n 6 >nul
    cls
    Echo Account,
    Echo %acctname%
    Echo created.
    Echo Press enter to finish!
    pause>nul
    cls
    > %acctname%.bat (
     Echo @Echo off
     Echo color 02
     Echo title %acctname%
     Echo Mode con cols=50 lines=30
     Echo :pass
     Echo cls
     Echo Echo Enter your password.
     Echo set ^/p password=
     Echo if %%password%%==%password% goto start
     Echo cls
     Echo Echo Sorry incorrect password.
     Echo Echo Enter to retry.
     Echo Pause^>nul
     Echo goto pass 
     Echo :start
     Echo cls
     Echo Echo welcome back,
     Echo Echo %acctname%
     Echo Echo Enter to begin playing.
     Echo Pause^>nul
     Echo set money=1000
     Echo cls
     Echo :tableselect
     Echo Echo Welcome to Blackjack.
     Echo Echo Money:$%%money%%
     Echo Echo.
     Echo Echo Please select a table.
     Echo.
     Echo Echo Table Number:1
     Echo Echo Dealer:David
     Echo Echo Betting Range:$10 - $50
     Echo Echo.
     Echo Echo Table Number:2
     Echo Echo Dealer:Jessica
     Echo Echo Betting Range:$50 - $100
     Echo Echo.
     Echo Echo Table Number:3
     Echo Echo Dealer:Mark
     Echo Echo Betting Range:$100 - $500
     Echo Echo.
     Echo Echo Table Number:4
     Echo Echo Dealer:Robert
     Echo Echo Betting Range:$500 - $1000
     Echo Echo.
     Echo set ^/p table=Table Number:
     Echo if %%table%%==1 goto Table1
     Echo if %%table%%==2 goto Table2 
     Echo if %%table%%==3 goto Table3
     Echo if %%table%%==4 goto Table4
     Echo :Table1
     Echo if %%money%% LEQ 0 goto NoMoney
     Echo cls
     Echo Echo Table Number:1
     Echo Echo Dealer:David
     Echo Echo Money:$%%money%%
     Echo Echo Players:
     Echo Echo.
     Echo Echo James:Bet:$0
     Echo Echo.
     Echo Echo Angie:Bet:$0
     Echo Echo.
     Echo Echo Jaryd:Bet:$0
     Echo Echo.
     Echo Echo %acctname%:Bet:$0
     Echo Echo.
     Echo set ^/p betamount=Bet Amount:$10 - $50:$
     Echo if %%betamount%% LEQ 9 goto invalidbet
     Echo if %%betamount%% GEQ 51 goto invalidbet
     Echo goto Table1active
     Echo :invalidbet
     Echo cls
     Echo Echo Sorry. Your bet was invalid.
     Echo Echo Enter to retry
     Echo Pause^>nul
     Echo goto Table1
     Echo :Table1active
     Echo set ^/a money=%%money%%-%%betamount%% 
     Echo cls
     Echo Echo Table Number:1
     Echo Echo Dealer:David
     Echo Echo Money:$%%money%%
     Echo Echo Players:
     Echo Echo.
     Echo Echo James:Bet:$%%betamount%%
     Echo Echo.
     Echo Echo Angie:Bet:$%%betamount%%
     Echo Echo.
     Echo Echo Jaryd:Bet:$%%betamount%%
     Echo Echo.
     Echo Echo %acctname%:Bet:$%%betamount%%
     Echo Echo.
     Echo Echo Press enter to start dealing.
     Echo pause^>nul
     Echo cls
     *****Echo set handP1=%%random%% %%21+4*****
     *****Echo set handP2=%%random%% %%21+4*****
     *****Echo set handP3=%%random%% %%21+4*****
     *****Echo set handP4=%%random%% %%21+4*****
     Echo Echo Table Number:1
     Echo Echo Dealer:David
     Echo Echo Money:$%%money%%
     Echo Echo Players:
     Echo Echo.
     Echo Echo James:Hand:%%handP1%%
     Echo Echo.
     Echo Echo Angie:Hand:%%handP2%%
     Echo Echo.
     Echo Echo Jaryd:Hand:%%handP3%%
     Echo Echo.
     Echo Echo %acctname%:Hand:%%handP4%%
     Echo Echo.
     Echo Echo Press 1 to hit.
     Echo Echo Press 2 to stay.
     Echo set ^/p hitStay=Choice:
     Echo if %%handP1%% LEQ 16 set ^/a handP1=%%handP1%%+%%random%% %11+1
     Echo if %%handP2%% LEQ 16 set ^/a handP1=%%handP1%%+%%random%% %11+1
     Echo if %%handP3%% LEQ 16 set ^/a handP1=%%handP1%%+%%random%% %11+1
     Echo if %%hitStay%%==1 goto hitT1
     Echo if %%hitStay%%==2 goto stayT1
     Echo :hitT1
     Echo set ^/a handP4=%%handP4%%+%%random%% %11 +1
     Echo cls
     Echo Table Number:1
     Echo Echo Dealer:David
     Echo Echo Money:$%%money%%
     Echo Echo Players:
     Echo Echo.
     Echo Echo James:Hand:$%%handP1%%
     Echo Echo.
     Echo Echo Angie:Hand:$%%handP2%%
     Echo Echo.
     Echo Echo Jaryd:Hand:$%%handP3%%
     Echo Echo.
     Echo Echo %acctname%:Hand:$%%handP4%%
     Echo Echo.
     Echo :stayT1
     Echo cls
     Echo Table Number:1
     Echo Echo Dealer:David
     Echo Echo Money:$%%money%%
     Echo Echo Players:
     Echo Echo.
     Echo Echo James:Hand:$%%handP1%%
     Echo Echo.
     Echo Echo Angie:Hand:$%%handP2%%
     Echo Echo.
     Echo Echo Jaryd:Hand:$%%handP3%%
     Echo Echo.
     Echo Echo %acctname%:Hand:$%%handP4%%
     Echo Echo.
     Echo Echo Please Wait.
     Echo ping localhost -n 5 >nul
     Echo if %%handP1%% GTR %%handP2%%

     Echo pause^>nul
     )
    Echo Account Created.
    Echo Please wait!
    Ping localhost -n 4 >nul
    start call %acctname%.bat
    exit


    :load
    cls
    Echo Type acct name
    Echo then hit enter.
    set /p acctname=
    start call %acctname%.bat
    exit

请帮我弄清楚为什么这不起作用。数字不在21和4之间。数字显示为34324 +4,或其他一些高数字+4

1 个答案:

答案 0 :(得分:1)

替换

Echo set handP1=%%random%% %%21+4

Echo set /A handP1=%%random%% %%%% 21 + 4

<强>证明

==>type D:\bat\SO\31377293.bat
@ECHO OFF >NUL
> files\31377293.bat (
  Echo set /A handP1=%%random%% %%%% 21 + 4
  Echo @set handP
)
@ECHO ON >NUL
type files\31377293.bat
call files\31377293.bat

<强>输出

==>D:\bat\SO\31377293.bat

==>type files\31377293.bat
set /A handP1=%random% %% 21 + 4
@set handP

==>call files\31377293.bat

==>set /A handP1=8932 % 21 + 4
handP1=11

==>