Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 批处理IF语句是否总是返回false?_Windows_Batch File_If Statement_Windows 7_Cmd - Fatal编程技术网

Windows 批处理IF语句是否总是返回false?

Windows 批处理IF语句是否总是返回false?,windows,batch-file,if-statement,windows-7,cmd,Windows,Batch File,If Statement,Windows 7,Cmd,好的,我有这个: :ask SET /p answer = Hello user. Do you want to use this program? (y/n) IF [/i] %answer% == y GOTO yes IF [/i] %answer% == n (GOTO no) ELSE (ECHO Your input was not accepted. Please try again. & GOTO ask) :yes ... ... :no pause 问题是,

好的,我有这个:

:ask
SET /p answer = Hello user. Do you want to use this program? (y/n) 

IF [/i] %answer% == y GOTO yes
IF [/i] %answer% == n (GOTO no) ELSE (ECHO Your input was not accepted. Please try again. & GOTO ask)

:yes
...
...

:no
pause
问题是,if语句返回false,并且它不断地请求我的输入。我希望这样,如果答案不是y或n,用户必须重新输入他们的答案

有什么帮助吗


谢谢

删除
回答
后的空格:

SET /p answer= Hello user. Do you want to use this program? (y/n)
在原始代码中,您正在创建一个名为
%answer%
的变量,而不是
%answer%

编辑如果条件,请删除
中的矩形括号

:ask
@echo off
SET /p answer= Hello user. Do you want to use this program? (y/n) 

IF /i "%answer%" == "y" GOTO :yes
IF /i "%answer%" == "n" (
 GOTO :no
) ELSE ( 
 ECHO Your input was not accepted. Please try again.
 GOTO :ask
)

:yes
rem

:no
rem
pause

删除等号左侧的空格,以避免声明名称中带有空格的变量。添加引号以避免某些字符(&|)出现问题

IF命令中的忽略大小写不包括[]。另外,添加引号以避免出现空答案的问题

IF /i "%answer%"=="y" GOTO yes

(叹气).
=
谢谢,但现在它说的是,在第一个条件语句
之后显示[my input]时是不需要的,如果[/i]
您的代码中是否使用了这些矩形括号?尝试删除它们,仅当/I
发生相同情况时才保留它们:/谢谢。是的,两种方法都有效。万一出现类似问题,有没有工具可以用来检查代码的语法错误?
IF /i "%answer%"=="y" GOTO yes