2013-12-09 29 views
0

我发现本教程在Notepad ++上制作游戏,并使批处理文件在CMD上运行。那么这里是我到目前为止:如何只选择可用的选项批处理文件

:ChooseWeapon 

cls 

echo "I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest." 

echo. 

set /p weapon=What is your weapon? (Sword, Double-Bladed Axe, Dagger): 

这一点是通过键入你想要的选择一个你想使用的武器。现在,事情是我可以输入“ghregff”,它会说这是我的武器。我该如何做到这一点,所以你必须选择:剑,双刃斧或匕首?

回答

1

您可以将它设置为如下所示的选项菜单,如果他们选择的数字不是1 2或3,它会将其踢回来再次输入选择。

@echo off 

:chooseweapon 
cls 
echo "I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest." 
echo. 
echo What is your weapon? (Sword, Double-Bladed Axe, Dagger): 
echo 1 - Sword 
echo 2 - Double-Bladed Axe 
echo 3 - Dagger 
echo. 

set /P Weapon="Enter a choice: " 
echo -------------------------------------------------------------------- 
for %%I in (1 2 3) do if #%Weapon%==#%%I goto wp%%I 
goto chooseweapon 

:wp1 
Set weapon=Sword 
goto end 

:wp2 
Set weapon=Double-Bladed Axe 
goto end 

:wp3 
Set weapon=Dagger 
goto end 

:end 
echo %weapon% 
pause 
0
@echo off 
set "NumberWeapons=3" 

:ChooseWeapon 
color 07 
cls 
echo."I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest." 
echo. 
echo.1 Sword 
echo.2 Double-Bladed Axe 
echo.3 Dagger 
set /p Weapon=What is your weapon? (1-%NumberWeapons%): 
echo. 
if %Weapon% lss 1 goto :ChoiceError 
IF %Weapon% gtr %NumberWeapons% goto :ChoiceError 
GOTO :Start 

:ChoiceError 
COLOR CF 
ECHO.Error! Choose a valid choice. 
pause 
goto :ChooseWeapon 

:Start 
echo.Replace this line with the rest of your stuff & pause 
0
@ECHO OFF 
SETLOCAL 
SET setprompt=What is your weapon? 
CALL :chooselist Sword, "Double-Bladed Axe", Dagger 
ECHO(Choice made was %response% 
CALL :chooselist2 Sword, "Double-Bladed Axe", Dagger 
ECHO(Choice made was %response% 
CALL :choosemenu Sword, "Double-Bladed Axe", Dagger 
ECHO(Choice made was %response% 
GOTO :EOF 

:chooselist 
SET valid=%* 
SET "nchoices=" 
CALL :choose "%setprompt% (%valid:"=%):" 
GOTO :eof 

:chooselist2 
SET valid=%* 
SET "nchoices=" 
FOR %%Z IN (%*) DO ECHO %%~Z 
CALL :choose "%setprompt%:" 
GOTO :eof 

:choosemenu 
SET valid=%* 
SET /a nchoices=0 
FOR %%Z IN (%*) DO SET /a nchoices+=1&CALL ECHO %%nchoices%% : %%~Z 
CALL :choose "%setprompt%:" 
GOTO :eof 

:choose 
SET /p "response=%~1 " 
IF NOT DEFINED response GOTO choose 
IF DEFINED nchoices FOR /l %%Z IN (1,1,%nchoices%) DO IF "%%Z"=="%response%" CALL :setresp %valid%&GOTO :eof 
SET /a cmatch=0 
CALL :countresp %valid% 
IF NOT %cmatch%==1 GOTO choose 
GOTO :eof 

:setresp 
IF %response% neq 1 SET /a response-=1&shift&GOTO setresp 
SET response=%~1 
GOTO :eof 

:countresp 
SET $1=%~1 
IF NOT DEFINED $1 (
IF %cmatch%==1 (SET response=%$3%) 
GOTO :eof 
) 
CALL SET $2=%%$1:*%response%=%% 
IF /i "%response%%$2%"=="%$1%" SET /a cmatch+=1&SET "$3=%~1" 
shift&GOTO countresp 

GOTO :eof 

这是一个灵活的一段代码,可以让你做出选择三种方式NE:

使用chooselist,参数列表附加到setprompt和用户可以选择完整地输入任何响应,或者仅仅足以明确定义需求。 “S”将定义剑,比如,但是“做”将需要“双刃斧”和“打”的“匕首”,因为“d”是不明确的。

使用chooselist2是相似的,它只是列出了其独立的行各的选择,并要求输入。

最后,choosemenu也是这样,但是可以使用数字或明确的起始字符串来进行编号和选择。

的反应总是会在response

0

最简单的方法似乎通过findstr命令来这样做。

:Choice 
set /p weapon=What is your weapon? (Sword, Double-Bladed Axe, Dagger): 
echo %weapon%| findstr /r "^Sword$ ^Double-Bladed Axe$ ^Dagger$">nul 
if errorlevel 1 (
    echo %weapon% was sadly not an option. 
    goto :Choice 
) 
echo you chose %weapon%. 

(你需要^和$ ^表示线的开始,$表示行尾)

OR

如果你想使用该系统多次,你可以带参数使用FINDSTR

和一个返回值

:MakeChoice 
set /p answer= : 
echo %answer%| findstr /r "^%~2$ ^%~3$ ^%~4$ ^%~5$ ">nul 
if errorlevel 1 (
echo "%answer%" is sadly not a valid choice, choose again. 
goto :MakeChoice 
) 
set %~1=%answer% 
goto :eof 

可以比调用该函数是这样的:

echo choose a weapon(Sword, Axe, Dagger) 
call :MakeChoice chosenOption "Sword" "Double-Bladed Axe" "Dagger" 
echo you chose %chosenOption% 

这适用于1-4个变量,但如果你想了解更多,您可以随时添加^%〜6 $ ^%〜7个$ ... ^以后%〜$ 5。

相关问题