4

バッチ テキスト ベースのゲームを作成しようとしています。しかし、書き始めたばかりで、これまでに経験したことのない問題に遭遇しました。

:menu
:: the game menu - opens when the game starts
cls
echo This game is still being made -- expermintal
echo Start Screen:
echo [1] View Changes
echo [2] Start Game
echo enter your choice:
set /p startchoice =
if %startchoice%==1 goto changes
if %startchoice%==2 goto startgame

1 または 2 のいずれかを入力すると、「goto は現時点では予想外でした」というエラーが表示されます。どうすれば修正できますか?

4

4 に答える 4

8

startchoice正しく設定されていません。set /pそこでプロンプトを提供する場所に別の構文を使用します(そしてstartchoice、と代入(=)演算子の間のスペースを削除します-実際にはそれが問題の原因だと思いますが、set /p <variable>=<Prompt>構文を使用するとバッチファイルを1行減らすことができます) .

goto, andステートメントに 2 つのターゲットを追加して、echo到達したことを確認できるようにしました。

:menu
:: the game menu - opens when the game starts
cls
echo This game is still being made -- expermintal
echo Start Screen:
echo [1] View Changes
echo [2] Start Game
set /p startchoice=Enter your choice:

if %startchoice%==1 goto changes
if %startchoice%==2 goto startgame
:changes
echo Changes
goto end
:startgame
echo StartGame
:end
于 2012-12-29T01:29:15.747 に答える
6

if 比較を引用符で囲む必要があり、プロンプトなしで set / p を使用するのは好きではありませんでした。以下の作品:

:menu
:: the game menu - opens when the game starts
cls
echo This game is still being made -- expermintal
echo Start Screen:
echo [1] View Changes
echo [2] Start Game
set /p startchoice = "enter your choice: "
if "%startchoice%"=="1" goto changes
if "%startchoice%"=="2" goto startgame
于 2012-12-29T01:29:35.357 に答える
0

環境変数を使用する代わりに、これを試してください。

CHOICE
IF %ERRORLEVEL% EQU 1 goto changes
IF %ERRORLEVEL% EQU 2 goto startgame

Choice は、数字または y/n を入力してエラー コードを返すプログラムです。%ERRORLEVEL% は、プログラムの最後のエラー コードを保持する変数です。

他の比較もできます。

EQU  - equal 
NEQ - not equal 
LSS - less than 
LEQ - less than or equal 
GTR - greater than 
GEQ - greater than or equal 
于 2012-12-29T01:30:09.987 に答える