简体   繁体   English

进入 goto 在当时是出乎意料的

[英]Getting goto was unexpected at the time

I am currently making a game that has a persuasion system in it.我目前正在制作一款带有说服系统的游戏。 I had all the code for one of the systems set up, but then I set up 2 more, and it started give me an error that said '(number) was not expected at this time'.我已经设置了其中一个系统的所有代码,但后来又设置了 2 个,它开始给我一个错误,说“此时不期望(数字)”。 when I put in 2 for the second choice, and 3 for the 3rd choice.当我为第二个选择输入 2,为第三个选择输入 3 时。

The code is like this.代码是这样的。

@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (

set "DEL=%%a"

)
set name=Quantum

cls 

color 0a

Echo King Redwood: So 2000?

pause >nul

echo.

call :colorText 09 "1. 2500"

echo.

call :colorText 0e "2. 3000"
echo.
call :colorText 0c "3. 4000"
echo.
echo 4. 2000
echo.
set /p "purs=Enter:"
if /i %purs% == 1 ( 
goto CheckB )
if /i %purs% == 2 ( 
goto CheckY )
if /i %purs% == 3 (
goto CheckR )
if /i %purs% == 4 ( 
goto Convo )


:CheckB
set bleu=%random:~-2,1%
if %bleu% GTR 10 (
goto CheckB )
if %bleu% LSS 0 (
goto CheckB )
set /a  num = 3
set /a  reward = 2500
goto Res
:CheckY
set Yel=%random:~-2,1%
if %Yel% GTR 10 (
goto CheckY )
if %Yel% LSS 0 (
goto CheckY )
set /a num = 5
set reward = 3000
goto Res

:CheckR
set red=%random:~-2,1%
if %red% GTR 10 (
goto CheckB )
if %red% LSS 0 (
goto CheckB )
set /a num = 7
set /a reward = 4000
goto Res

:Convo
set /a reward = 2000
Echo %name%: I think that is a reasonable price.
Echo King Redwood: Very well.
Echo King Redwood: We will now take you to make sure you are
echo ready.
pause >nul


:Res
if %bleu% GEQ %num% goto Succeed
if NOT %bleu% GEQ %num% goto Fail

:Succeed
Echo %name%: I think that the struggles for such a long trip will be more then that
Echo %name%: How about %reward%? 
Echo King Redwod: OK %reward% will work.
pause >nul 
goto end

:Fail
Echo %name%: I think that you can give me %reward%.
Echo %name%: You know, for the struggles that there will be along the way.
echo If 2000 isn't good enough for you, I'll just have someone else do it.
pause >nul

:end
exit


:colorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

First, make sure to close the FOR loop by putting a ) before :CheckB.首先,确保通过在 :CheckB 之前放置 ) 来关闭 FOR 循环。

For the 'was not expected at this time' error, you're sometimes comparing an empty variable to something.对于“此时未预期”错误,您有时会将空变量与某些内容进行比较。 For example, by following CheckY, you set Yel, then proceed to Res and check Bleu, which is empty because it hasn't been set.例如,按照 CheckY 设置 Yel,然后转到 Res 并检查 Bleu,它是空的,因为它尚未设置。 You're putting nothing next to the binary GEQ operator, and that's why it's complaining.您没有在二元 GEQ 运算符旁边放置任何东西,这就是它抱怨的原因。

Tip: to debug, try inserting ECHO statements like this:提示:要调试,请尝试插入这样的 ECHO 语句:

:Res
echo bleu=%bleu%,num=%num%

Another problem: when using SET, do not surround the = by spaces.另一个问题:使用 SET 时,不要用空格将 = 括起来。 SET /a will work with spaces around =, just because of the nature of /a, but plain SET will not. SET /a可以处理 = 周围的空格,只是因为 /a 的性质,但普通的 SET 不行。 Well, it will append your variable name with a space and prepend your value with a space, which is not what you want.好吧,它会在你的变量名后面加上一个空格,并在你的值前面加上一个空格,这不是你想要的。

Another tip: you can constrain what RANDOM returns through SET /a and the modulus operator, like this.另一个提示:您可以像这样通过 SET /a 和模运算符来限制 RANDOM 返回的内容。

SET /a red=%random% %% 11

This will set red to a number between 0 and 10, so there is no need for the substrings and goto routines you're using after picking your random number.这会将红色设置为 0 到 10 之间的数字,因此在选择随机数后不需要您使用的子字符串和 goto 例程。

Also, consider using EXIT /b to exit the batch file and not the whole CMD environment.另外,请考虑使用EXIT /b退出批处理文件而不是整个 CMD 环境。

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

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