这段代码旨在类似于口袋妖怪战斗游戏的简单版本。我只在攻击中编码。我一直在彻底测试,发现每当用户确认他们的攻击时都会出现错误消息(此时 Goto 是意外的(。警告!!代码长 96 行。最后,我将放置问题部分,因此您可以跳过第一个大块。
@echo off
Set H1=20
Set A1=8
Set D1=6
Set S1=5
Set H2=14
Set A2=5
Set D2=4
Set S2=8
:Begin
CLS
Echo Bulbasur
Echo %H2%/14 /
Echo (__) ___
Echo l __lo.ol
Echo l_ l_"
Echo.
Echo _
Echo * /
Echo \l )
Echo \__l Charmander
Echo %H1%/20
Echo -Attack -Capture
Echo -Item -Run
Set /p Move=Action?
If %move%==Attack goto Attack
If %move%==Catpure goto capture
If %move%==Item goto Item
If %move%==Run Goto Run
Echo I'm sorry, Charmander can't do that.
Pause
goto Begin
:Attack
ClS
Echo Attacks
Echo 1)Tackle
Echo 2)Growl
Echo 3)Ember
Echo 4)Scratch
Set /p attack=Which one?
If %attack%==Tackle goto Tackle
If %attack%==1 goto Tackle
If %attack%==Growl Goto Growl
If %attack%==2 goto Growl
If %attack%==Ember goto Ember
If %attack%==3 goto Ember
If %attack%==Scratch goto Scratch
If %attack%==4 goto Scratch
If %attack%==Cancel goto Begin
Echo I didn't get that
Goto Attack
:Tackle
CLS
Echo Tackle Hits The opponent where it hurts. EVERYWHERE.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Tackle
:Growl
CLS
Echo Growl lowers the opponents attack.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Status
If %acccept%==No goto Begin
Echo I didn't get that.
goto Growl
:Scratch
CLS
Echo Scratch hits the foe with a claw.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Scratch
:Ember
CLS
Echo Ember hits the opponent with a small fire.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Ember
:Combat
CLS
If NOT %attack%==Growl If NOT %attack%==2 set /a H2=%H2%-(%A1%^2/%D2%)
set /a H1=%H1%-(%A2%^2/%D1%)
goto Begin
:Status
CLS
Set /a A1=%A1%-1
goto Combat
问题领域:
:Tackle
CLS
Echo Tackle Hits The opponent where it hurts. EVERYWHERE.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Tackle
代码在这里很好,但是一旦我在这里,它就不需要goto命令。有人能解决这块牛肉吗?(注意:铲球只是一个例子。这些攻击都不起作用。编辑:如果用户输入"是","否",胡言乱语或什么都没有,它仍然会传递相同的错误消息(此时goto是意外的(
你必须把它放在引号里:
if "%accept%"=="yes" goto combat
if "%accept%"=="no" goto begin
或者更确切地说,如果您不想使其区分大小写:
if /i "%accept%"=="yes" goto combat
if /i "%accept%"=="no" goto begin
你的问题是这一行:
set /p accept=Yes/No?
不使用与这些变量相同的变量名称:
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
上面的变量有"ccc",但第一个只是"cc">
编辑
嗨,用户1205760;我有一些时间可以花,所以我把你的程序做得更小一些。这是我的版本:
@echo off
Setlocal EnableDelayedExpansion
Set Actions=Attack Capture Item Run
Set Attacks=Tackle Growl Ember Scratch Cancel
Set i=0
For %%a in (%Attacks%) do set /A i+=1 & set Attack[!i!]=%%a
Set H1=20
Set A1=8
Set D1=6
Set S1=5
Set H2=14
Set A2=5
Set D2=4
Set S2=8
:Begin
:Cancel
CLS
Echo Bulbasur
Echo %H2%/14 /
Echo (__) ___
Echo l __lo.ol
Echo l_ l_"
Echo.
Echo _
Echo * /
Echo \l )
Echo \__l Charmander
Echo %H1%/20
Echo -Attack -Capture
Echo -Item -Run
Set /p Move=Action?
For %%a in (%Actions%) do if /I %move%==%%a goto %move%
Echo I'm sorry, Charmander can't do that.
Pause
goto Begin
:Attack
Cls
Echo Attacks
For %%a in (1 2 3 4) do echo %%a)!Attack[%%a]!
Set /p attack=Which one?
for %%a in (1 2 3 4) do if %attack%==%%a set attack=!Attack[%%a]!
for %%a in (%Attacks%) do if /I %attack%==%%a goto %attack%
Echo I didn't get that
Pause
Goto Attack
:Tackle
call :Confirm Tackle Hits The opponent where it hurts. EVERYWHERE.
If %accept%==Yes goto Combat
goto Begin
:Growl
call :Confirm Growl lowers the opponents attack.
If %accept%==Yes goto Status
goto Begin
:Ember
call :Confirm Ember hits the opponent with a small fire.
If %accept%==Yes goto Combat
goto Begin
:Scratch
call :Confirm Scratch hits the foe with a claw.
If %accept%==Yes goto Combat
goto Begin
:Status
Set /A A1-=1
:Combat
If /I NOT %attack%==Growl set /A H2=H2-(A1^2/D2)
set /A H1=H1-(A2^2/D1)
goto Begin
:Confirm
Cls
Echo %*
Echo Do you want to?
set /p accept=Yes/No?
For %%a in (Yes No) do if /I %accept%==%%a exit /B
Echo I didn't get that.
Pause
goto Confirm
如果用户不输入任何内容,则您的If
行的计算结果可能如下所示:
…
If ==Yes goto Combat
If ==No goto Begin
…
这在语法上是不正确的。我建议在 set /p
命令之前使用一些默认值初始化accept
:
…
set accept=default
set /p accept=Yes/No?
if …
这样,如果用户只按 Enter 键,accept
变量将保留default
值,后续if
将不会以错误告终。
对不起,
这只是对反对票的一个问题。
只是问的人不确定问题是否出在那部分。
例如
set a=
if %a%==1 echo yes
如果我只是发布这一行:
if %a%==1 echo yes
那么每个人都会知道问题出在哪里吗?
请记住,对于变量,如 %abc%,最好将其与 ",[ 或 { 一起使用,以防止出现错误消息。
例如
set /p abc=
用户什么也没输入。
那么下一行应该是:
if %abc%==1 echo Hi
但它变成了:
if ==1 echo Hi
,作为 "%abc%"==">
但是使用",它将变成
if ""=="1" echo Hi
和"不等于"1"。
理解?
编辑---
如果您使用的是 Windows 7(或其他版本(,您也可以尝试以下操作:
choice /c YN /n /m "Confirm? [Y^|N]
^
只是逃离"管道"(|
(。
希望这对你有用!