简体   繁体   English

goto此时出乎意料

[英]goto was unexpected at this time

@echo off
color 0a
title Horror Game
echo.
echo.
echo.
echo.
echo Welcome to the game
echo If you get scared
echo Feel free to leave
echo.
echo.
echo You are in a dark room.
echo It is cold.
echo All you hear is a scratching sound
echo near your feet.
echo What do you do?
echo.
echo.
echo 1.) Feel around you
echo 2.) Listen for anything else
set/p input = Command?
if %input% == "1" goto Feel
if %input% == "2" goto Listen
echo.
echo.
:Feel
echo You feel around and hear a growl.
echo As you realize the scratching was
echo on your leg.
echo. 
echo You remember nothing else.
pause
end

I am trying to make a text based game for cmd and whenever i try to enter a response is instantly closes and i can barely read out "goto was unexpected at this time"我正在尝试为 cmd 制作一个基于文本的游戏,每当我尝试输入时,响应都会立即关闭,我几乎无法读出“goto was unexpected at this time”

You did not form your tests correctly. 您没有正确地形成测试。 You need to enclose the variable in double-quotes: 您需要将变量括在双引号中:

if "%input%" == "1" goto Feel
if "%input%" == "2" goto Listen

Note that you need an extra condition after these to deal with the possibility that they didn't enter 1 or 2. 请注意,在这些之后你需要一个额外的条件来处理他们没有输入1或2的可能性。

You should consider using the choice command instead of set /p . 您应该考虑使用choice命令而不是set /p Type choice /? choice /? from the command prompt. 从命令提示符。

In future, run your script from the command prompt if you're having trouble with premature termination. 将来,如果您在提前终止时遇到问题,请从命令提示符运行脚本。 That way the window won't close on you and you'll have plenty of time to read the error. 这样窗口就不会关闭,你将有足够的时间来阅读错误。

You can run a command prompt quickly by pressing 您可以按下快速运行命令提示符 Windows键 - R , typing cmd and pressing Enter - R ,输入cmd并按Enter键

You should delete the space between input and = in the set command, otherwise you're actually setting a variable with the name input (with a space at the end). 你应该在set命令中删除input=之间的空格,否则你实际上是设置一个名为input的变量(末尾有一个空格)。 This works as expected: 这按预期工作:

echo 1.) Feel around you
echo 2.) Listen for anything else
set/p input=Command?
if %input% == "1" goto Feel
if %input% == "2" goto Listen
echo.
echo.
:Feel
echo You feel around and hear a growl.
echo As you realize the scratching was
echo on your leg.
echo. 
echo You remember nothing else.

However, without quotation marks around %input% this will produce the unexpected goto error if the user enters an input with a space in it. 但是,如果用户输入带有空格的输入,则在%input%周围没有引号,这将产生unexpected goto错误。 paddy's suggestion to use the choice command is probably the way to go. paddy建议使用选择命令可能是要走的路。

Also note that when you are in a Windows File Explorer window, you can type "cmd" into the File Explorer address bar, push, and this will produce the Command Prompt with the prompt path already navigated to the folder currently being viewed in the File Explorer window (presumably the folder with your batch file).另请注意,当您在 Windows 文件资源管理器窗口中时,您可以在文件资源管理器地址栏中键入“cmd”,然后按下,这将生成命令提示符,提示路径已导航到文件中当前正在查看的文件夹资源管理器窗口(可能是包含批处理文件的文件夹)。 Then you can type the name of your batch file to run it.然后您可以键入批处理文件的名称来运行它。 As someone previously mentioned, you can more easily see your error codes this way instead of the window closing out, but this method (as opposed to using the Windows key - R) gives you a better way to get to the command prompt since you won't have to fuss with any "CD" commands to get to the folder you want.正如之前提到的那样,您可以通过这种方式更轻松地查看错误代码,而不是关闭窗口,但是这种方法(与使用 Windows 键 - R 相反)为您提供了一种更好的方式来进入命令提示符,因为您赢了不必大惊小怪地使用任何“CD”命令来访问您想要的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM