批量游戏退出而不是下一行

时间:2013-04-22 03:48:50

标签: windows if-statement batch-file cmd set

每当我尝试播放时,它会退出而不是在我进入时进入下一行;它一直正常工作,直到你输入你的名字,然后它跳到问题1,然后它退出。我不是这方面的专家所以我想知道我做错了什么以及如何解决它。如果您发现任何其他错误,请随时指出。谢谢! :)

@echo off
color F0
cls
echo.
echo True or False
pause
echo Welcome! May I ask your name before we begin?
echo.
set /p name=
echo.
echo Hello %name%, nice to meet you!
echo.
echo My name is Myst.
echo.
cls
echo.
echo Let us begin!
:start
color FC
cls
echo.
echo QUESTION 1
pause
echo.
echo Though hard, you can start a fire by rubbing 2 cool ranch
   doritos together  for a long time.
echo.
echo TRUE or FALSE
echo.
set /p variable=
echo.
if %variable% equ TRUE goto question2 if %variable% equ FALSE goto
   answer1
if %variable% neq TRUE goto start
:answer1
cls
echo.
echo Wrong! Though it is very hard, it is possible.
goto start
:question2
color F3
cls
echo QUESTION 2
pause
echo.
echo Singing in the shower lowers your cholesterol, heart rate, &
   risk of cancer  and heart disease.
echo.
echo TRUE or FALSE
echo.
set /p variable=
echo.
if %variable% equ TRUE goto answer2
if %variable% equ FALSE goto question3
if %variable% neq TRUE goto question2
:answer2
cls
echo.
echo Wrong!
echo.
echo Here is a fast fact for you %name%. Dark chocolate doesn't
   either.
goto start
:question3
color F5
cls
echo QUESTION 3
pause
echo.
exit

3 个答案:

答案 0 :(得分:2)

试试这个:

if "%variable%" equ "TRUE" goto :question2
if "%variable%" equ "FALSE" goto :answer1
goto :start

答案 1 :(得分:2)

您遇到了一些语法错误和各种问题。试试这个例子:它有一些变化和编辑,使它更健壮。

@echo off
color F0
cls
echo.
echo True or False
echo.
set /p "name=Welcome! May I ask your name before we begin? "
echo.
echo Hello %name%, nice to meet you!
echo.
echo My name is Myst.
echo.
echo.
ping -n 3 localhost >nul
echo Let us begin!
pause
:start
color FC
cls
echo.
echo QUESTION 1
echo.
echo Though hard, you can start a fire by rubbing 2 cool ranch
echo doritos together for a long time.
echo.
echo. 
set /p "variable=TRUE or FALSE: "
echo.
if /i "%variable%" equ "TRUE" (
echo Right!
pause
goto :question2
)
cls
echo.
echo Wrong! Though it is very hard, it is possible.
pause
:question2
color F3
cls
echo QUESTION 2
echo.
echo Singing in the shower lowers your cholesterol, heart rate, &
echo risk of cancer and heart disease.
echo.
set /p "variable=TRUE or FALSE: "
echo.
if /i "%variable%" equ "FALSE" (
echo Right!
pause
goto :question3
)
echo 
cls
echo.
echo Wrong!
echo.
echo Here is a fast fact for you %name%. Dark chocolate doesn't
echo either.
pause
:question3
color F5
cls
echo QUESTION 3
pause
echo.
exit

答案 2 :(得分:1)

echo Hello %name%, nice to meet you!
echo.
echo My name is Myst.
echo.
  

CLS

echo.
echo Let us begin!

CLS命令用于清理窗口,因此您无需读取任何内容。您还需要停止执行脚本(暂停)。

您正在使用cls命令太多次而不暂停代码。

此外,你需要尝试不要制作这样的可理解代码,你需要制作缩进,制作空行,让我们理解代码。

并且不需要使用“真或假”之类的字符串识别?因为存在一种名为“Boolean”的变量并且只有True / False,所以可以在Choice命令中使用布尔问题。

你真的需要重新编写所有代码,因为你可以看到这样的事情:

if %variable% equ TRUE goto answer2
if %variable% equ FALSE goto question3
if %variable% neq TRUE goto question2

如果变量只能是“true”和“False”那么第三个条件永远不会被处理,不需要专业人员理解它,是合乎逻辑的。

考虑一下如果用户键入“true”或“TrUe”或任何变体会发生什么?

然后你需要使用“IF”语句的/ I参数。

If /I EQU "True" (Goto...) ELSE (Goto...)

你正在使用运营商“&”使用echo命令打印字符串,但不能使用双序或转义字符来打印运算符:

Echo "&"
Echo ^&

太多的事情需要解释,但是你的代码的一大问题是你错过了读取字符串所需的10“暂停”。

PS:对不起我的英文。

以下是更正后的代码:

@Echo off
Color F0

Echo+
Echo: True or False | MORE
Pause & CLS

Echo+
Echo: Welcome! May I ask your name before we begin? | MORE
set /p "name=" & CLS

Echo+
Echo: Hello %name%, nice to meet you! | MORE
Echo: My name is Myst.                | MORE
Pause & CLS

Echo+
Echo: Let us begin!
Pause & CLS

:start
color FC
Echo+
Echo: QUESTION 1
Pause
Echo+
Echo: TRUE or FALSE | MORE
Choice /C TF /M "Though hard, you can start a fire by rubbing 2 cool ranch doritos together for a long time."
if %ERRORLEVEL% EQU 1 (Goto Question2) ELSE (goto Answer1)

:answer1
cls
Echo+
Echo: Wrong! Though it is very hard, it is possible.
Pause & CLS
Goto :start

:question2
color F3
cls
Echo: QUESTION 2
pause
Echo+
Echo: TRUE or FALSE | MORE
Choice /C TF /M "Singing in the shower lowers your cholesterol, heart rate, & risk of cancer and heart disease."
if %ERRORLEVEL% EQU 1 (Goto answer2) ELSE (goto question3)

:answer2
cls
Echo+
Echo: Wrong! | MORE
Echo: Here is a fast fact for you %name%. Dark chocolate doesn't either.
Pause & CLS
goto start

:question3
...
...
...