在批处理文件中使用If语句

时间:2014-05-08 00:44:18

标签: batch-file cmd command-prompt

我有一个批处理脚本(以“战争游戏”中的约书亚为模型)

@echo off
color 0b
echo Greetings, Professor Falken
set /p interface =
echo Would You Like to Play a Game?
set /p ifGame =
if /i "%ifGame%" =="yes" goto yesgame
if /i "%ifGame%" =="no" goto nogame
:yesgame
echo List of Games
echo chess
echo Poker
echo Fighter Combat
echo Guerilla Warfare
echo Desert Warfare
echo Air-to-Ground Actions
echo Theaterwide Tactical Warfare
echo Theaterwide Biotoxic and Chemical Warfare
echo Global Thermonuclear War
pause
echo Which game would you like to play?
set /p WhichGame =
pause
exit
:nogame
set /p areYouSure=Are You Sure? 
pause
exit

但是,当我输入“No”时,它仍会显示游戏列表......

1 个答案:

答案 0 :(得分:3)

使用=时,您需要删除变量名称和set /p符号之间的空格。从:

set /p ifGame =

要:

set /p ifGame=

否则,您设置的变量在名称末尾有一个空格。因此%ifGame%扩展为空,而%ifGame %将扩展为正确的值。