【问题标题】:Goto was unexpected at this time batch fileGoto 在这个时候出乎意料的批处理文件
【发布时间】:2013-12-05 04:00:16
【问题描述】:

请使用以下脚本作为参考。 好吧,我正在尝试进行编程,并且正在创建批处理 rpg 以查看是否可以做到...无论如何,有点碰壁:我不断收到“此时 goto 出乎意料”错误.错误如下:最初,在标签游戏(:game)之后,输入系统工作,但不正确(即您输入输入4得到8的操作,输入8没有得到任何东西,等等)。我改变了一些东西,但我不确定我改变了什么......在到达标签游戏时导致“错误:此时 goto 是意外的”。我意识到我在错误的地方添加了括号,所以我再次更改了它(在 %location%==1 之后)所以我更正了它 - 现在,在接受输入“items”(set /p items=">")之后,它呈现相同的错误,但没有显示背景故事。有趣的是,如果我在' set /p towna="> " ' 之后删除所有“if”参数,程序结束(没有错误,只是结束)而不显示“items”之后的背景故事,镇上可能的动作, 或允许用户输入变量 %towna%

脚本如下:

@echo off
title=Role Playing Game
cls
echo/
echo   #####     #####     ####     
echo   #    #    #    #   #
echo   #####     #####    #  ###
echo   #    #    #        #    #
echo   #     #   #         ####
echo/
echo/
pause
:menu
cls
echo MENU
echo Options:
echo Start New Game-------------1
echo Continue Previous Game-----2
echo About----------------------3
echo Quit-----------------------4
set /p a="> "
if %a%==1 goto new
if %a%==2 goto continue
if %a%==3 goto about
if %a%==4 exit
:new
cls
echo What is your name?
set /p name="> "
echo/
echo What is your race?
echo Human-----1
echo Dwarf-----2
echo Elf-------3
set /p race="> "
if %race%==1 (
set /a location=1
set /a agility=3
set /a strength=3
set /a magic=2
set /a stamina=0
)
if %race%==2 (
set /a location=2
set /a agility=0
set /a strength=6
set /a stamina=0
set /a magic=0
)
if %race%==3 (
set /a location=3
set /a strength=0
set /a agility=6
set /a magic=0
set /a stamina=0
)
echo/
echo What is your class?
echo Warrior------1
echo Assassin-----2
echo Wizard-------3
set /p class="> "
if %class%==1 (
set /a strength=%strength%+10
set /a stamina=10
set /a agility=%agility%+7
set /a magic=%magic%+0
)
if %class%==2 (
set /a strength=%strength%+7
set /a stamina=7
set /a agility=%agility%+10
set /a magic=%magic%+0
)
if %class%==3 (
set /a strength=%strength%+4
set /a stamina=5
set /a agility=%agility%+5
set /a magic=%magic%+10
)
echo/
echo What are your assets?
echo Monetary Wealth----------1
echo Arms and Armor-----------2
echo Potions and Trinkets-----3
set /p items="> "
set /a gold=100
set /a sword=1
set /a armor=1
set /a healthp=1
set /a focusp=0
set /a ragep=0
set /a trinket=0
if %items%==1 (
set /a gold=300
)
if %items%==2 (
set /a sword=3
set /a armor=3
)
if %items%==3 (
set /a healthp=5
set /a focusp=5
set /a ragep=5
set /a trinket=1
)
echo/
pause
cls
if %race%==1 (
if %class%==1 (
if %items%==1 (
echo For all of your life, you have been raised among humans on the WesterHam
echo plains. An aristocrat by birth, you longed for the thrill of combat and
echo apprenticed your-self to the service of an officer in the military, where you
echo became an excellent warrior. Now of age, you have decided to take your
echo inheritance and explore the world. Good luck, %name%.
)
if %items%==2 (
echo You come from a long line of noble warriors, descending of a race of pure
echo humans out of the WesterHam plains. While you were not always able to put
echo bread on the table, your family has an abundance of arms and armor. Now fully
echo grown, you set out, well equiped with family heirlooms in order to make a
echo better life for yourself. Good luck, %name%.
)
if %items%==3 (
echo Your family of humans comes out of the WesterHam plains, and has never
echo ventured out of this area. As a child, you were fascinated with the art of
echo magic, but were never able to grasp spellcasting and the like, and instead
echo took training as a warrior. But the fascination never died, and every bit of
echo money you earned as a child was spent buying potions and enchanted items. Now
echo grown, you have saved a small sum of money, in addition to several potions
echo you collected as a child. You set out to explore the world in search of a
echo magic art simple enough for you to grasp. Good luck, %name%.
)
)
if %class%==2 (
if %items%==1 (
echo In your early childhood, you were raised with the honor of your wealthy
echo human family of WesterHam. As you met adolescence, you rebeled and cast your 
echo lot with the assassins, allowing them to teach you their ways. Now of legal
echo age, you demand your inheritance so that you may set off and prove your loyalty
echo to the assassins guild. Good luck, %name%.
)
if %items%==2 (
echo As a child, you grew up in the human-populated plains of WesterHam. You were
echo orphaned at a young age, but the assassins guild took you in and taught you
echo their ways. From them, you have recieved a set of arms with which to do the
echo work of an assassin. Now completely trained, you begin to search the world,
echo offering your services to those that pay. Good luck, %name%.
)
if %items%==3 (
echo As a child, your upbringing was among humans in the plains of WesterHam. You
echo had a natural skill in alchemy, and began to produce potions of sorts, which
echo you would sell on the black market in hopes of bringing some income for your
echo poor family. In this style of trade, you were introduced to assassins from
echo time to time. In order to help sustain your family, you joined the assassins
echo guild in hopes of increasing income. Now armed with a collection of potions,
echo you set off to make a new life. Good luck, %name%.
)
)
if %class%==3 (
if %items%==1 (
echo As a child, you were brought up as a wealthy human inhabitant of the WesterHam
echo plains. You proved to be very intelligent as a child, so your parents sent you
echo off to school to learn the magic arts. You learned quickly, and advanced much
echo faster than any of the other students. You are now of age, and have taken your
echo inheritance to set off and see how you may use your skills. Good luck,
echo %name%.
)
if %items%==2 (
echo Ever since you were young, you displayed a strong curiosity and sense of
echo intelligence. However, you came from a family of warriors, proud humans of the
echo WesterHam plains. Weapons you had, but the money to learn magic you had not.
echo instead, you were "self taught" in the arts of magic, aquiring what you know
echo by close, constant observation of those that had mastered the art. Now of age,
echo you have inherited your set of arms, and been sent off to make your own way in
echo the world. Good luck, %name%.
)
if %items%==3 (
echo Since your days of young, you have been entirely enthrolled with wizardry.
echo Having been raised as an honorable human wizard on the plains of WesterHam, you
echo learned to produce potions that would complement your magical talents. Now that
echo you are of age, you set off into the world, determined to rely on use of your
echo magic talents and the aid of potions. Good luck, %name%.
)
)
)
if %race%==2 (
if %class%==1 (
if %items%==1 (
echo For all of your life, you have been raised among dwarves in the Morrowvale
echo mines. Wealthy by birth, you longed for the honors of battle and enlisted in
echo defensive forces of Morrowvale, where you soon proved your talents. Now of age,
echo you have taken your inheritance to explore the world beyond Marrowvale. Good
echo luck, %name%.
)
if %items%==2 (
echo You come from a long line of noble warriors, descending of a race of dwarvish
echo folk out of the Morrowvale mines. While you were not always able to put bread 
echo on the table, your family has an abundance of arms and armor. Now fully grown,
echo you set out, well equiped with family heirlooms in order to make a better life
echo for yourself. Good luck, %name%.
)
if %items%==3 (
echo Your family of dwarves comes out of the Morrowvale mines, and has never
echo ventured out of this area. As a child, you were fascinated with the art of
echo magic, but were never able to grasp spellcasting and the like, and instead
echo took training as a warrior. But the fascination never died, and every bit of
echo money you earned as a child was spent buying potions and enchanted items. Now
echo grown, you have saved a small sum of money, in addition to several potions
echo you collected as a child. You set out to explore the world in search of a
echo magic art simple enough for you to grasp. Good luck, %name%.
)
)
if %class%==2 (
if %items%==1 (
echo In your early childhood, you were raised with the honor of your wealthy
echo family of dwarves in Morrowvale. As you met adolescence, youre good natured 
echo sense of adventure led you mistakenly to the assassins guild, where you learned
echo their ways. Now of age, you recieve your inheritance so that you may set off to
echo explore the world. Good luck, %name%.
)
if %items%==2 (
echo As a child, you grew up in the dwarf-populated mines of Morrowvale. You were
echo orphaned at a young age, but the assassins guild took you in and taught you
echo their ways. From them, you have recieved a set of arms with which to do the
echo work of your training. Now completely trained, you begin to search the world,
echo offering your services in the name of justice. Good luck, %name%.
)
if %items%==3 (
echo As a child, your upbringing was among dwarves in the mines of Morrowvale. You
echo "tinkered" some in alchemy, and began to produce potions of sorts, which you
echo would sell on the black market in hopes of bringing some income for your poor
echo family. In this style of trade, you were introduced to assassins from time to
echo time. In curiosity you joined the assassins guild. Now armed with a collection
echo of potions, you set off to explore the world. Good luck, %name%.
)
)
if %class%==3 (
if %items%==1 (
echo As a child, you were brought up as a well off dwarf of the Morrowvale mines.
echo You proved to be very intelligent as a child, so your parents sent you off to
echo school to learn the magic arts. You learned quickly, and advanced much faster
echo faster than any of the other dwarves. You are now of age, and have taken your
echo inheritance to set off and see how you may use your skills. Good luck,
echo %name%.
)
if %items%==2 (
echo Ever since you were young, you displayed a strong curiosity and sense of
echo intelligence. However, you came from a family of warriors, proud dwarves of
echo the Morrowvale mines. Arms you had, but the money to learn magic you had not.
echo instead, you were "self taught" in the arts of magic, aquiring what you know
echo by close, constant observation of elders who had mastered the art. Now of age,
echo you have inherited your set of arms, and been sent off to make your own way in
echo the world. Good luck, %name%.
)
if %items%==3 (
echo Since your days of young, you have been consumed with wizardry. Having been
echo raised as a witty dwarfish wizard on the mines of Morrowvale, you learned to
echo produce potions that would complement your magical talents. Now that you are of
echo age, you set off into the world, determined to rely on use of your magic talent
echo and the aid of potions. Good luck, %name%.
)
)
)
if %race%==3 (
if %class%==1 (
if %items%==1 (
echo For all of your life, you have been raised among elves in the Dalewood forests.
echo An aristocrat by birth, you wished to serve your people in the armed forces,
echo and quickly proved yourself among peers and superiors. Now of age, you have
echo a generous inheritance and skills in combat. You set off in conquest for the 
echo elvish people. Good luck, %name%.
)
if %items%==2 (
echo You come from a long line of noble warriors, descending of a race of pure
echo elves out of the Dalewood forests. While you were not always able to put bread
echo on the table, your family has an abundance of arms and armor. Now fully grown,
echo you set out, well equiped with family heirlooms in order to make a better life
echo for yourself. Good luck, %name%.
)
if %items%==3 (
echo Your family of elves comes out of the Dalewood forests, and has never ventured
echo out of this area. As a child, you were fascinated with the art of magic, but
echo were never able to grasp spellcasting and the like, and instead took training
echo as a warrior. But the fascination never died, and every bit of coin you earned
echo as a child was spent buying potions and enchanted items. Now grown, you have
echo saved a small sum of money, in addition to several potions you collected as a
echo child. You set out to explore the world in search of a magic art simple enough
echo for you to grasp. Good luck, %name%.
)
)
if %class%==2 (
if %items%==1 (
echo In your early childhood, you were raised with the honor of your wealthy elvish
echo family of Dalewood. As you met adolescence, you chose the honorable path of a 
echo royal assassin, being taught the ways of a fearless shadow on the battlefield.
echo As you are now of age, you aquire your inheritance so that you may set off and
echo prove your loyalty to elf-kind. Good luck, %name%.
)
if %items%==2 (
echo As a child, you grew up in the elf-populated forests of Dalewood. You were
echo chose your destiny at a young age, to be an elite assassin for the elvish army.
echo From them, you have recieved a set of arms with which to do the work of an
echo assassin. Now completely trained, you begin to search the world, using your
echo assets in the name of elf-kind. Good luck, %name%.
)
if %items%==3 (
echo As a child, your upbringing was among elves in the forests of Dalewood. You
echo had a natural skill in alchemy, producing fine elvish potions, which you would
echo sell for the commision of the elite elvish assassins. This introduction to the
echo assassins life intrigued you, and soon you began your training as an assassin.
echo Now having completed your training, you head off armed with a collection of
echo potions, to make a new life. Good luck, %name%.
)
)
if %class%==3 (
if %items%==1 (
echo As a child, you were brought up as a wealthy elvish inhabitant of the Dalewood
echo forests. You proved to be very intelligent as a child, so your parents sent you
echo off to the academy to learn the magic arts. You learned quickly, and advanced
echo faster than any of the other students. You are now of age, and have taken your
echo inheritance to set off and see how you may use your skills. Good luck,
echo %name%.
)
if %items%==2 (
echo Ever since you were young, you displayed a strong curiosity and sense of
echo intelligence. However, you came from a family of warriors, proud elves of the
echo Dalewood forests. Weapons you had, but the money to learn magic you had not.
echo instead, you were "self taught" in the arts of magic, aquiring what you know
echo by close, constant observation of those that had mastered the art. Now of age,
echo you have inherited your set of arms, and been sent off to make your own way in
echo the world. Good luck, %name%.
)
if %items%==3 (
echo Since your days of young, you have been entirely enthrolled with wizardry.
echo Having been raised as an honorable elvish wizard in the forests of Dalewood,
echo you learned to produce potions that would complement your magical talents. Now
echo that you are of age, you set off into the world, determined to rely on use of
echo your magic talents and the aid of potions. Good luck, %name%.
)
)
)
echo/
:game
cls
if %location%==1 (
echo Plains of WesterHam
echo/
echo You walk down the fading dirt road into the prarie town of WesterHam. The wind
echo blows the long, thin grass about in a ripple-type of pattern. As you enter the
echo town, you look about and note the busy market, characteristic of towns
echo inhabited by humans. You see a quaint looking inn directly to your left, and a
echo ways down the road you spot a bakery. On the right side or the street is a
echo blacksmith shop, as well as an apothecaries shop that advertises instruction in
echo magic as well. At the very end of the lane, you percieve a church of moderate
echo size.
echo/
echo/
echo What do you want to do?
echo Enter the inn---------------------1
echo Enter the bakery------------------2
echo Enter the blacksmith shop---------3
echo Enter the apothecaries------------4
echo Enter the church------------------5
echo Enter the market-place------------6
echo Loot some homes-------------------7
echo Turn around; travel elsewhere-----8
set /p towna="> "
if %towna%==1 goto inn
if %towna%==2 (
cls
echo Seems to be closed today. Maybe the owner is at the market-place.
pause
goto town1i
)
if %towna%==3 goto smith
if %towna%==4 goto apoth
if %towna%==5 goto church
if %towna%==6 (
cls
echo You don't have time for such things. Maybe in the Beta version you will.
pause
goto town1i
)
if %towna%==7 (
cls
echo No, youre too good for that. Maybe in the Beta version you won't be.
goto town1i
)
if %towna%==8 goto travel
)
if %location%==2 (
echo Mines of Morrowvale
)
if %location%==3 (
echo Forests of Dalewood
)

据我所见,我的代码应该可以工作,但我不知道是什么导致了错误消息。作为旁注,什么可能导致错误消息“goto is unexpected”?有未定义的目标吗?是否可以通过添加目标标签来解决这个问题(不太可能,因为这会显示标签未定义的消息)。以后如何避免这种情况?

【问题讨论】:

  • Windows 批处理文件语言非常有限 - 尝试使用它编写程序时您会感到非常沮丧。除非有一些令人信服的理由你需要学习这种特定的语言(提示:没有),我强烈建议你选择另一种。
  • @duskwuff 我知道它是有限的,我只是想在我转向更高级的东西之前精通批处理脚本。
  • 老实说,这不是一门非常有用的语言。如果你现在真的想为 Windows 系统编写脚本,那么使用 Powershell 会好得多。批处理文件是 80 年代的遗物。
  • 我尝试过其他脚本语言,例如 Java 和 C++,但我过去的编码经验是 TI Basic 和 Z80 汇编,所以语法一点也不熟悉。我真的使用批处理作为从图形计算器到 PC 的过渡。与其他脚本语言相比,它是有限的,但我在这里离开了 TI-84。

标签: batch-file


【解决方案1】:

您的问题是批次扩展得太早了。

当您的程序到达if %location%==1 行时,将展开整个以下if 块,包括嵌套的if towna==* 行。

要修复,您必须执行以下操作:

  1. 你必须在开始时调用SETLOCAL ENABLEDELAYEDEXPANSION 你的代码。此命令启用动态的延迟扩展 变量。

  2. 然后您必须使用if !towna!==* 而不是if %towna%==*。这个 一种变量在到达块后不会立即扩展。 相反,如果 SETLOCAL ENABLEDELAYEDEXPANSION 已被调用, 当它被物理到达时,它将被扩展 口译员。

请注意,当您在任何语句块中设置和调用变量时,必须执行这些步骤。

【讨论】:

  • @Daemon- 我试过这个,它在某种程度上起作用了......当我输入 towna 的输入(即 1 到 8)时,它没有显示未定义标签的错误,当参数需要一个标签,当参数需要显示文本时,没有显示任何文本(加上它完全跳过了背景故事)。但这比我得到的好。但是除了阅读关于批处理的维基百科页面之外,我没有接受过脚本方面的教育,所以你能解释一下这里发生了什么吗?
  • 尝试在您希望用户阅读的每个文本块之后暂停。在背景故事echo 调用之后的cls 调用出现在用户有机会阅读它之前。尝试重新审视您的脚本并进行战略性的pause 调用,让用户控制游戏何时进行。
  • 感谢您指出这一点,我不敢相信我忽略了这一点。当我试图找出问题时,我一定在某个时候改变了这一点。所以我认为 goto 错误在背景故事之前就已经出现了,而不是因为背景故事闪得太快而无法看到。为什么启用延迟扩展和!必须在块内使用,不能在块外使用?
  • 这是由于批处理块的方式。批处理脚本使用 JIT (Just-In-Time) 扩展执行。这意味着在解释器到达之前不会扩展一行代码。但是,当到达一个块时,该代码会立即展开,具体取决于块条件是否计算为TRUE。我记得这是由于批处理如何处理块,因为它会处理单行,但我可能记错了。我会调查一下并回复你。
  • 我研究了help set,看来我没记错。当遇到IF 语句时,会读取整个IF 块,因为它在逻辑上包含在“复合语句”中。
【解决方案2】:

主要问题是您没有告诉我们您在提示中输入了什么,因此跟踪未知输入会发生什么意味着您必须为我们的水晶球提供新电池。

一般来说,在处理输入时,您应该注意两个特征。首先,只需按 Enter 将保持目标变量不变。您可以通过使用来利用它

set var=defaultvalue
set /p var="> "

这将在 Enter 上将var 设置为默认值

或

set "var="
set /p var="> "
if not defined var echo no input?

如果您不想要默认值。

第二个问题是批处理在执行前将%var% 替换为该变量的当前值。因此,如果var 未设置(或“设置”为空字符串),if %var%==something whatever 将变为if ==something whatever。此外,如果var 设置为“带有空格的东西”,则会尝试执行if something with spaces==something whatever . CMD 由于历史原因需要语法 IF string operator string dosomething withparameters,因此您通常需要使用 if "%var%"=="string"... 来分析用户输入。

语法!var! 需要在“块语句”(即带括号的语句序列)中使用新值。 %var% 将为您提供**OLD** 值(即,在执行开始之前,第一次遇到调用块的语句时变量的原始值。)要使用!var!,您需要处于延迟扩展中模式,即你需要已经执行了setlocal enabledelayedexpansion语句。


另外,一个小的未更改的段段

set /p towna="> "
if %towna%==1 goto inn
if %towna%==2 (
cls
echo Seems to be closed today. Maybe the owner is at the market-place.
pause
goto town1i
)
if %towna%==3 goto smith
if %towna%==4 goto apoth
if %towna%==5 goto church
if %towna%==6 (
cls
echo You don't have time for such things. Maybe in the Beta version you will.
pause
goto town1i
)
if %towna%==7 (
cls
echo No, youre too good for that. Maybe in the Beta version you won't be.
goto town1i
)
if %towna%==8 goto travel
)        <<<<<<<<THIS STRAY ) can cause havoc!

说明%var% 和!var! 在块内部和外部以及有/没有enabledelayedexpansion 之间的区别的附录

@ECHO OFF
SETLOCAL
set var=before
if "%var%"=="before" (
 set var=after
 echo in-block results: %var% and !var!
)
echo after block results: %var% and !var!
ENDLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO DELAYEDEXPANSION invoked
set var=before
if "%var%"=="before" (
 set var=after
 echo in-block results: %var% and !var!
)
echo after block results: %var% and !var!
ENDLOCAL
GOTO :EOF

【讨论】:

  • 那个括号用来括住“if %location%==1 (”,没有它我会得到“
  • 哇! - 好吧,我当然不会对例程进行那么长时间的语法检查。然而,问题在于有问题的命令在一个块中 - 这似乎是一个非常大的块。在块内,您需要调用enabledelayedexpansion 并使用!var! 来访问块内更改的变量的值。我相信是时候学习子程序了。
  • 好的,成功了。我在脚本开始时使用了 enabledelayedexpansion,它需要在块内使用。所以只是基本问题: enabledelayedexpansion 发生了什么(什么是解析)?用 !! 包围变量有什么区别?相对 %% ?一个块中的所有动作是否都不管顺序执行(除非启用延迟扩展)?
  • 解析本质上是对命令进行预处理以确保它在语法上是正确的。
  • 谢谢,现在说得通了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 2012-04-04
  • 2011-12-07
  • 1970-01-01
相关资源
最近更新 更多