Mik*_*ele 3 variables if-statement batch-file
希望你们感恩节快乐!
无论如何,我一直在寻找答案,但我似乎无法找到我需要的答案.
所以基本上我的批处理文件有一个输入,你在其中写一个句子.句子将带你:正确,只要它有"妈妈陈"字样.
@echo off
:start
set /p test=Write a sentene with the word Mommy Chan in it:
if "%test%"=="Mommy Chan" goto correct
if "%test%" NEQ "Mommy Chan" goto incorrect
:correct
cls
echo %test%
echo you did it
pause
goto start
:incorrect
cls
echo %test%
echo you didn't follow directions
pause
goto start
Run Code Online (Sandbox Code Playgroud)
现在,这似乎有效并带给你:如果用户输入单词"Mommy Chan"作为输入,则更正.然而,假设用户输入"Not Mommy Chan",它似乎没有认识到包含"妈咪陈"这个词的事实,它会带你到:不正确
显然我不希望这样.
为了澄清,我想要它,如果你输入任何带有"妈妈陈"字样的句子,例如:"有一天,妈妈陈去购物",它应该带你去:正确.但是,只有当用户只输入"妈妈陈"时它才会这样做,否则它只会带你:不正确
任何人都知道如何解决这个问题?
提前致谢.
一种可能的方式:
@echo off
:start
set /p test=Write a sentene with the word Mommy Chan in it:
::replaces "Mommy Chan" in %test% to see if it was changed
if "%test:Mommy Chan=%" equ "%test%" goto incorrect
if "%test:Mommy Chan=%" neq "%test%" goto correct
exit /b 0
:correct
echo correct
exit /b 0
:incorrect
echo incorrect
exit /b 0
Run Code Online (Sandbox Code Playgroud)
另一个(可能更慢,因为它调用find.exe):
@echo off
:start
set /p test=Write a sentene with the word Mommy Chan in it:
echo %test%|find "Mommy Chan" >nul 2>nul && (
goto :correct
color
)||(
goto incorrect
)
exit /b 0
:correct
echo correct
exit /b 0
:incorrect
echo incorrect
exit /b 0
Run Code Online (Sandbox Code Playgroud)
对于不区分大小写的检查-既IF和FIND使用/I开关.