variables - 如果 a/p 输入包含特定单词

标签 variables batch-file if-statement

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

无论如何,我一直在寻找这个问题的答案,但似乎找不到我需要的答案。

所以基本上我的批处理文件有一个输入,您可以在其中写一个句子。只要有“陈妈”字样的句子就会带你:纠正。

@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

现在,这似乎有效,并且如果用户在输入中输入“Mommy Chan”一词,而不是其他任何内容,那么您将得到:正确。然而,假设用户输入“Not Mommy Chan”,它似乎没有意识到包含“Mommy Chan”一词的事实,并且它会带你:不正确

显然我不想这样。

为了澄清,我希望这样,如果您输入任何包含“Mommy Chan”一词的句子,例如:“有一天,Mommy Chan 去购物”,它应该让您得到:正确。 但是,只有当用户仅输入“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

另一个(可能较慢,因为它调用 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 

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

关于variables - 如果 a/p 输入包含特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34023792/

相关文章:

c - 变量值不存储

javascript - 通过 javascript 传递 PHP 变量并再次返回

xml - 使用 sqlcmd ":r"将 XML 文件的内容获取到变量中

batch-file - 带参数批量启动 exe

java - 程序因文本字段为空而崩溃

bash - bash 与 ksh 中的变量重定向

windows - 帮助编写批处理脚本来解析 CSV 文件并输出文本文件

Java If 循环未执行 : Scanner

javascript - 有没有办法在 VUE html 中编写 "If Greater than"和 "If less than"语句?

batch-file - 如何使用批处理文件更改配置文件中的现有变量