转到意外错误

时间:2017-03-20 17:11:54

标签: batch-file

我正在尝试使用批处理脚本编写一个基本的琐事游戏,以便在我的笔记本电脑上的DOS CMD中运行。这只是一个实验性的事情,所以我可以练习使用批处理脚本。

第一个问题将在没有问题的情况下执行,但我无法执行第二个问题。它继续说,"在这个时候意外发现"。为了解决这个问题,有什么我需要注意的吗?

感谢您的协助。

代码:

@echo off
color 02
title PaleScream's Trivia Quiz
echo.
echo - - - - - - - - - - - - - - - 
echo.
echo Welcome to PaleScream's Trivia Quiz
echo.
echo - - - - - - - - - - - - - - - 
echo.
pause
cls
echo Who is that little green guy in Star Wars
echo 1 Yoda
echo 2 Darth Vader
echo 3 Luke
set /p starwars=choice1~3 
if %starwars%==1 goto Correct
if %starwars%==2 goto Incorrect
if %starwars%==3 goto Incorrect
:Correct
echo You got it right!!
pause
cls
goto continued
:Incorrect
echo Sorry you got it wrong!!
pause
goto end
:continued
echo What is the national animal of Scotland?
echo 1 Reindeer
echo 2 Unicorn
echo 3 Valais Blackneck Goat
set /p ScotlandAnimal = choice1~3 
if %ScotlandAnimal%==1 goto incorrect 
if %ScotlandAnimal%==2 goto correct
if %ScotlandAnimal%==3 goto incorrect
echo ----------------------------------
:correct
echo You got it right!!
pause 
goto continued
:incorrect
echo Sorry, try again.
pause
goto end
:continued
cls
echo you made it to the end
pause
:end

2 个答案:

答案 0 :(得分:1)

你的错是一次有一个带有var名称的尾随空格 set /p ScotlandAnimal = choice1~3
和内容,然后检查没有该尾随空格的var名称 if %ScotlandAnimal%==1 goto incorrect
由于没有空间的var不存在,因此它的计算结果为空,因此cmd解释器看到了
if ==1 goto incorrect
这是无效的语法。

答案 1 :(得分:1)

set /p更改为Choice并更改结构,以便您不需要多个goto包含相同/类似输出文本的标签,就可以提供更好的服务。< / p>

@Echo Off
Color 02
Title PaleScream's Trivia Quiz

Echo(
Echo(- - - - - - - - - - - - - - - 
Echo(
Echo(Welcome to PaleScream's Trivia Quiz
Echo(
Echo(- - - - - - - - - - - - - - - 
Echo(
Timeout -1

Rem Example with correct answer as 1
ClS
Echo(   1 Yoda
Echo(   2 Darth Vader
Echo(   3 Luke
Echo(----------------------------------
Choice /C 123 /M "Who is that little green guy in Star Wars "
If ErrorLevel 2 (Call :Incorrect & GoTo :EOF) Else If Errorlevel 1 (
    Call :Correct) Else GoTo :EOF

Rem Example with correct answer as 2
ClS
Echo(   1 Reindeer
Echo(   2 Unicorn
Echo(   3 Valais Blackneck Goat
Echo(----------------------------------
Choice /C 123 /M "What is the national animal of Scotland "
If ErrorLevel 3 (Call :Incorrect & GoTo :EOF) Else If Errorlevel 2 (
    Call :Correct) Else If ErrorLevel 1 (Call :Incorrect & GoTo :EOF
) Else GoTo :EOF

Rem Example with correct answer as 3
ClS
Echo(   1 Hungry Polar Bear
Echo(   2 Starving White Shark
Echo(   3 Partner with a credit card
Echo(----------------------------------
Choice /C 123 /M "Which is the most dangerous "
If ErrorLevel 3 (Call :Correct) Else If Errorlevel 1 (
    Call :Incorrect & GoTo :EOF) Else GoTo :EOF

Rem Place rest of questions here using format as above

Rem Do not change anything below here
ClS
Echo(You made it to the end
Echo(
Echo( Press any key to exit ...
Timeout -1 1>Nul
Exit/B

:Correct
Echo(You got it right!!
Timeout 2 1>Nul
ClS
GoTo :EOF

:Incorrect
Echo(Sorry you got it wrong!!
Timeout 2 1>Nul
ClS
GoTo :EOF