2017-03-20 33 views
0

バッチスクリプトを使用して、ノートパソコンのDOS CMDで実行する基本的なトリビアゲームを作成しようとしています。実験的なものだったので、バッチスクリプトを使って練習することができました。予期しないエラーが発生しました

最初の質問は問題なく実行されますが、2番目の質問は実行できません。それは、 "この時点で予期せぬことに行く"と続けている。これを解決するために私が見なければならないものがありますか?

ありがとうございました。

コード:

@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

DOSはCMDではありません....インターネットをチェック.... – SteveFest

答えて

1

あなたの障害が1時間にするvar名前の末尾のスペースを持つ
set /p ScotlandAnimal = choice1~3
と内容で、その後、あなたがその末尾にスペースなし
if %ScotlandAnimal%==1 goto incorrect
をVAR名を確認 スペースのないvarは存在しないため、何も評価されないので、cmdインタプリタは、
if ==1 goto incorrect
は無効な構文です。

+0

申し訳ありません、私の最初の言語はJavaScriptで余分な空白は問題ではありません。ありがとうございました! – 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

関連する問題