Board logo

标题: SET与IF的问题 [打印本页]

作者: panhong1986     时间: 2006-10-29 16:38    标题: SET与IF的问题

怎么用SET和IF结合起来做一个像CHOICE的命令. 我知道怎么做成用数字来选择, 像1 2 3 4之类的. 但是我想做一个可以用字母来选择, 比如Y, N. 请大家帮忙一起想想. 谢谢.
@ECHO off
cls
:start
ECHO.
ECHO 1. Print Hello
ECHO 2. Print Bye
ECHO 3. Print Test
set choice=
set /p choice=Type the number to print text.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto hello
if '%choice%'=='2' goto bye
if '%choice%'=='3' goto test
ECHO "%choice%" is not valid please try again
ECHO.
goto start
:hello
ECHO HELLO
goto end
:bye
ECHO BYE
goto end
:test
ECHO TEST
goto end
:end


──────────────── 版务记录 ────────────────
执行:namejm
原标题:SET与IF的问题
说明:因标题过于模糊,不便于论坛的搜索和管理,请在三日内修改标题。
提示:修改标题请在当前帖的右下脚点
编辑修改完毕之后按 编辑帖子 即可。
处罚:因刚脱离论坛新人阶段,暂时不予处罚;若三日之后尚未修改标题,将扣除4点积分,
      以示惩罚,并由版主强制修改标题。请点击
这里阅读论坛发帖规定,以避免在
      今后的讨论中违规发帖。
──────────────── 版务记录 ────────────────


[ Last edited by namejm on 2006-10-30 at 06:55 AM ]
作者: lxmxn     时间: 2006-10-29 18:12

  把里面的字符改一下就成了。如下:
@ECHO off
:start
cls
ECHO.
ECHO Y. Print Hello
ECHO N. Print Bye
ECHO C. Print Test
set choice=
set /p choice=Type the character to print text.Y[Yes]\N[No]\C[Cancel]
if not "%choice%"=="" set choice=%choice:~0,1%
if /i "%choice%"=="Y" goto hello
if /i "%choice%"=="N" goto bye
if /i "%choice%"=="C" goto test
ECHO "%choice%" is not valid please try again
ECHO.
goto start

:hello
ECHO HELLO&pause>nul
goto end

:bye
ECHO BYE&pause>nul
goto end

:test
ECHO TEST&pause>nul
goto end
:end