2017-03-20 1 views
0

내 랩톱의 DOS CMD에서 실행되는 기본 퀴즈 게임을 작성하기 위해 배치 스크립트를 사용하려고합니다. 그것은 실험적인 것일 뿐이므로 배치 스크립트를 좀 더 연습 할 수있었습니다.예기치 않은 오류로 이동

첫 번째 질문은 문제없이 실행되지만 두 번째 질문을 실행할 수 없습니다. 계속해서 "이 시간에 예기치 않은 것"이라고 말합니다. 이 문제를 해결하기 위해 내가 볼 필요가있는 것이 있습니까?

감사합니다.

코드 :

한 시간이 VAR 이름
set /p ScotlandAnimal = choice1~3
과 내용으로 후행 공백을 가진 후 해당 후행 공백
if %ScotlandAnimal%==1 goto incorrect
없이 VAR 이름을 확인하려면
@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 
+0

도스는 CMD가 아닙니다 .... 인터넷을 확인하십시오 .... – SteveFest

답변

1

당신의 잘못입니다 공간이없는 var가 존재하지 않으므로 아무 것도 평가하지 않으므로 cmd 인터프리터는
if ==1 goto incorrect
을 봅니다. 잘못된 구문입니다.

+0

죄송합니다, 제 첫 번째 언어는 자바 스크립트이며 여분의 공백은 문제가되지 않습니다. 고맙습니다! – PaleScream

1

set /pChoice으로 변경하고 동일/유사한 출력 텍스트가 포함 된 레이블에 goto을 여러 개 입력 할 필요가 없도록 구조를 변경하는 것이 더 나을 것입니다.

@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 
+0

EOF는 무엇을 의미합니까? 그리고 여러 번 다시 인쇄 할 필요없이 전체 코드에서 동일한 오류를 호출 할 수 있습니까? – PaleScream

+0

'GoTo : EOF'가 설명되어 있습니다 (http://stackoverflow.com/questions/37515901/where-does-goto-eof-return-to). 나는': Incorrect'와': Correct' 레이블이 한 번만 코딩되고 필요에 따라 호출되도록 코드 구조를 이미 설계했습니다. – Compo

+0

고맙습니다. 나는 그것을 밖으로 시도 할 것이다. – PaleScream

관련 문제