首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果/p输入中包含某个单词

如果/p输入中包含某个单词
EN

Stack Overflow用户
提问于 2015-12-01 15:19:21
回答 1查看 42关注 0票数 3

希望你们感恩节过得愉快!

不管怎样,我一直在四处寻找答案,但我似乎找不到我需要的答案。

基本上,我的批处理文件有一个输入,你可以在其中写一个句子。这句话会带你去:只要里面有“陈妈妈”这个词,就改正。

代码语言:javascript
复制
@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

现在,这似乎是可行的,并将您带到:如果用户输入的单词“妈妈陈”的输入正确,而没有其他。但是,如果用户输入“不是妈妈成龙”,它似乎没有意识到“妈妈成龙”这个词包含在其中,它会让你看到:不正确

显然我不想那样。

为了澄清问题,我想让你输入“妈妈陈”这个词的句子,例如:“有一天陈妈妈去购物”,它应该把你带到:更正。然而,只有当用户只输入“妈妈陈”时,它才会这样做,否则它只会让你:不正确。

有谁知道怎么解决这个问题吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-01 15:36:08

一种可能的方式是:

代码语言:javascript
复制
@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

另一个(可能更慢一些,因为它调用了find.exe):

代码语言:javascript
复制
@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 

对于不区分大小写的检查,IFFIND都使用/I开关。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34023792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档