且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

批次:转到在这个时候意外的错误

更新时间:2022-10-19 11:41:24

 如果选择%==%1页转到工资

想象一下,这是什么会做,当%选择%是空的。然后,线可能会被解读为:

 如果== 1页转到工资

显然,一个语法错误。 转到出人意料。

要避免这种情况,用

 如果选择%%= =1转到工资

这将被解读为:

 如果==1转到工资

语法现在是确定的。

So im making a project for school and i want to goto menu if the user input is not valid
My code is this:

:menu  
echo.  
echo.  
echo What would you like to know first?
echo.
echo.
echo 1) Salary
echo 2) What Do they do?
echo 3) Schools near me that offer game design programs
echo 4) Requirements
echo 5) Credits
echo.
echo.
set /p choice="Enter Number: "
if %choice% == 1 goto salary
if %choice% == 2 goto do
if %choice% == 3 goto schools
if %choice% == 4 goto req
if %choice% == 5 goto credits
goto menu

when i enter input, there is a line that flashes
Goto was unexpected at this time then exits
What am i doing wrong?
I should add that my input is not a valid option. (1,2,3,4,5)

if %choice% == 1 goto salary

Imagine, what this would do, when %choice% is empty. The line would then be read as:

if  == 1 goto salary

Obviously a syntax error. "goto was unexpected".

To avoid this, use

if "%choice%" == "1" goto salary

This would be read as:

if "" == "1" goto salary

Syntax is ok now.