Batch file 批处理的If/Else语句
我正在尝试用Batch/CMD创建一个RPG游戏。我希望这样,当用户输入的数字不是列出的数字时,它只会从最后一个标记重新加载。代码如下:Batch file 批处理的If/Else语句,batch-file,if-statement,cmd,Batch File,If Statement,Cmd,我正在尝试用Batch/CMD创建一个RPG游戏。我希望这样,当用户输入的数字不是列出的数字时,它只会从最后一个标记重新加载。代码如下: if %1st%==1 %atk%+25 if %1st%==2 %def%+25 if %1st%==3 %hp%+25 if %1st%==4 %iq%+25 if %1st%==5 %luck%+25 if not %1st%==1 goto 1st if not %1st%==2 goto 1st if not %1st%==3 goto
if %1st%==1 %atk%+25
if %1st%==2 %def%+25
if %1st%==3 %hp%+25
if %1st%==4 %iq%+25
if %1st%==5 %luck%+25
if not %1st%==1 goto 1st
if not %1st%==2 goto 1st
if not %1st%==3 goto 1st
if not %1st%==4 goto 1st
if not %1st%==5 goto 1st
最好不要让变量以数字开头,因为
%1
表示传递到脚本中的第一个参数。如果没有向脚本传递任何参数,解释器会将这些语句视为无效的If not st%==1 goto 1st
和If st%==1%atk%+25
您需要用建议的%first%
替换所有%1st%
,其余部分如下:%2nd%
到%second%
%3rd%
至%3rd%
%4th%
至%4th%
%5th%
至%5th%
%6th%
至%6th%
%7th%
至%7th%
%8th%
至%8th%
%9th%
至%9th%
以下是您使用上述更改编写的代码:
if %first%==1 %atk%+25
if %first%==2 %def%+25
if %first%==3 %hp%+25
if %first%==4 %iq%+25
if %first%==5 %luck%+25
if not %first%==1 goto first
if not %first%==2 goto first
if not %first%==3 goto first
if not %first%==4 goto first
if not %first%==5 goto first
我不确定您是如何添加这些值的,下面是一个关于set/a
命令的建议更改:
if %first%==1 set /a atk=%atk%+25
if %first%==2 set /a def=%def%+25
if %first%==3 set /a hp=%hp%+25
if %first%==4 set /a iq=%iq%+25
if %first%==5 set /a luck=%luck%+25
if not %first%==1 goto first
if not %first%==2 goto first
if not %first%==3 goto first
if not %first%==4 goto first
if not %first%==5 goto first
好啊什么是有效的,什么是无效的?它不断地破坏程序,而不是返回到最后一个标记。非常感谢!我会记住不要使用带有数字的值!