Confusion with Batch-file code

时间:2016-04-21 22:33:20

标签: batch-file

So recently I started learning Batch-file, and today I decided to try and make a game with it. I have a few errors, and can't figure out what is wrong with the code. Heres the code:

@echo off
title Batch Rpg
color 07

echo Welcome to Batch Rpg!
echo.
echo.
pause

:menu
cls
echo.
echo    -Menu-
echo.
echo.
echo.
echo    1) Begin
echo.
echo    2) How to play
echo.
echo    3) Exit
echo.
echo.
echo.
echo    ---------
echo.
    set /p c=Choice: 
        if %c%==1 goto prestart1
        if %c%==2 goto howtoplay
        if %c%==3 goto cfr_exit
        if NOT %c%==1 if NOT %c%==2 if NOT %c%==3 goto menu


:cfr_exit
cls
echo.
echo    Are you sure you want to exit?
echo.
    set /p c=(Y/N): 
        if %c%=="Y" exit
        if %c%=="N" goto menu
        if NOT %c%=="Y" if NOT %c%=="N" goto cfr_exit2

:cfr_exit2
cls
echo.
echo    You must enter a valid option.
echo.
pause
goto cfr_exit

:howtoplay
cls
echo.
echo    -How to play-
echo.
echo.
echo.
echo    This game is very simple. There will be a number with an option after it, type the option in and it will perform an action(As the option would say).
echo.
pause
goto menu

:prestart1
cls
echo.
echo    Welcome to land of Fageryth!
echo.
echo    What is your name, adventurer?
echo.
    set /p playername=Name: 
goto prestart2

:prestart2
cls
echo.
echo    What would be your more valued statistic, Attack damage, or Hit points?
echo.
echo.
echo.
echo    1)Attack damage(Atk)
echo.
echo    2)Hit points(Hp)
echo.
echo.
echo.
echo    ---------
echo.
    set /p playermorevaluedstat=Choice: 
        if %playermorevaluedstat%==1
            set playeratk=6
            set playerhp=25
        if %playermorevaluedstat%==2 
            set playeratk=4
            set playerhp=30
        if NOT %playermorevaluedstat%==1 if NOT %playermorevaluedstat%==2 goto prestart2
cls
echo playeratk
echo playerhp
pause

I'm having trouble with the :prestart2 section of my code. With the end of it, I tried to make it where if the variable wasn't equal to 1 or 2, which were the options, then it send the player back up to the start of the section again, and also, when it finishes checking, I'm trying to make it display the two variables playeratk and playerhp but instead it just closes out. I am really lost here and would appreciate the help!

1 个答案:

答案 0 :(得分:2)

我们开始前的几件事:

首先,在对批处理脚本进行疑难解答时,请删除(注释掉)echo offcls行,以便了解它出错的位置。

其次,您应该始终对变量进行双引号,以确保在设置和比较它们时不会无意中包含空格。它似乎并没有在您的代码中造成问题,但这是一个很好的习惯:

set "var1=something"
set "var2=a string with spaces"
if "%var1%"=="something" echo %var1%

现在,代码中的问题是两个if语句在多行上延伸。如果您要这样做,则必须将它们放在括号内。

set /p playermorevaluedstat=Choice: 
        if %playermorevaluedstat%==1 (
            set playeratk=6
            set playerhp=25
            )
        if %playermorevaluedstat%==2 (
            set playeratk=4
            set playerhp=30
            )