为什么我的批处理文件显示所有变量

时间:2017-06-20 10:03:26

标签: batch-file

我正在编写基于文本的批处理RPG,但代码读取所有格式为:VARIABLE的变量 回声 组 如果 等

好像是一系列代码。

CODE:

:start
@echo off
set /p name=Welcome to the game, what is your name?:
if %name%==ChrisPBacon goto crispy
ping localhost -n 2 >nul
cls
goto selectclass


:WARRIORSELECT1
cls
set /p question1=Your class is Warrior, is this correct Y/N?:
if %question1%==Y cls goto skillselect1
if %question1%==N cls goto selectclass

:PALADINSELECT1
cls
set /p question1=Your class is Paladin, is this correct Y/N?:
if %question1%==Y cls goto skillselect1
if %question1%==N cls goto selectclass

:MAGESELECT1
cls
set /p question1=Your class is Mage, is this correct Y/N?:
if %question1%==Y cls goto skillselect1
if %question1%==N cls goto selectclass

:CLERICSELECT1
cls
set /p question1=Your class is Cleric, is this correct Y/N?:
if %question1%==Y cls goto clerichealer
if %question1%==N cls goto selectclass


:selectclass
echo Welcome to the game, %name%!
ping localhost -n 2 >nul
echo Warrior
echo Paladin
echo Mage
echo Cleric
set /p CLASS=What class would you like to be?:
if %CLASS%==Warrior goto WARRIORSELECT1
if %CLASS%==Paladin goto PALADINSELECT1
if %CLASS%==Mage goto MAGESELECT1
if %CLASS%==Cleric goto CLERICSELECT1


:clerichealer
set /p surecleric=Are you sure you wish to be the Cleric? This class does 0 
damage and can only heal Y/N!:
if %surecleric%==Y cls goto skillselect1
if %surecleric%==N cls goto selectclass

:skillselect1
echo Welcome to the skill selection menu, %Name% the %CLASS% From here you 
can select your initial skills, with 10 points to spend at first and more 
can be gained by levelling up!
echo Weight (W)
echo Attack Damage (AD)
echo Magic Damage (MD)
echo Healing Effectiveness (HE)
set /p point1 What attribute do you wish to level up with your 1st point?
if point1==W echo Increased weight by +10!
if point1==AD echo Increased Attack Damage by +20!
if point1==MD echo Increased Magic Damage by +15!
if point1==HE echo Increased Healing Effectiveness by +10 Health!














:crispy
echo Welcome Admin!
echo 
echo 1) Force Delete current save
echo 2) Implement Preset save
set /p bacon=What do you wish to do?

特别是,当选择了除Cleric之外的任何类时,它会遍历所有类,询问这是否是您选择的类。当选择牧师时,它会循环回欢迎游戏,%name%等。

2 个答案:

答案 0 :(得分:2)

看看这个片段。

:CLERICSELECT1
cls
set /p question1=Your class is Cleric, is this correct Y/N?:
if %question1%==Y cls goto clerichealer
if %question1%==N cls goto selectclass


:selectclass
  • 如果您的选择不是YN,则会自动继续,然后到达:selectclass。整个脚本都会出现此问题,因此请先修复它们。

  • 大多数新批处理脚本所做的另一件事是一个if语句中的多命令。你可以使用

if "a"=="b" cls && goto blah

if "a"=="b" (
    cls
    goto blah
)
  • 请注意我添加的引号",因为引号有助于处理带空格的变量。

  • 您可以选择将/i标记添加到if,这意味着不区分大小写搜索。

  • 当我们想要echo一个空行时,我们不仅使用echo,而是使用echo(,尽管有一些不太安全的方法。< / p>

  • 某些变量未包含在%符号中。

  • 除非您需要echo循环

  • ,否则多行回送需要多for
  • 其中一个set /p声明中有一个缺少等号

  • 将密码直接存储在批处理文件中是不安全的,即使在将其设为.exe文件之后也是如此,因为大多数bat to exe converter确实是临时的{{}临时目录中的1}}文件,恶意用户可以轻松访问。

答案 1 :(得分:2)

当你选择神职人员时让它循环的事情就是它会继续下去,即使你进入&#34; n&#34; (而不是&#34; N&#34;),正如SteveFest所说。

但您的代码存在许多问题:

  • 它没有考虑不正确或空的输入
  • if比较区分大小写
  • if比较
  • 没有引号
  • 很明显,如果你继续使用过多的goto语句会随着时间的推移而变得更糟糕