首页 > 解决方案 > 创建商店。不会正确减去钱

问题描述

我希望能够在商店中购买商品并让它做正确的减法。在下面的代码中,您从10金币 (gp) 开始,但无论何时输入 option24,来花费5gp 或1gp,它都会带走所有10gp。我知道这是因为它没有超过第一个if %input%== 1但我不知道如何修复它,我已经尝试了几乎所有的东西,包括if/else语句,但我可能没有正确地做它们

:shop
cls
echo You see a middle aged man behind the counter
echo of the shop as well as a younger man sweeping the floors.
echo "Hello young travelers. Welcome, is there anything
echo I can help you find?"
:purchase
echo --------------------------------------------------------
echo %name%
echo Gold: %gp%
echo --------------------------------------------------------
echo.
echo 1) Battleaxe   10gp  Stats: 1d8(S) Versatile(1d10)
echo 2) Mace         5gp  Stats: 1d6(B)
echo 3) L.Crossbow  20gp  Stats: 1d8(P) Range 80/320
echo 4) 5 Bolts      1gp  Equip with Crossbow
echo 5) Go Back
echo.
set /p input=Enter:

if %input%== 5 goto main
if %input%== 1
if %gp% LSS 10 goto nofunds
set /a gp= %gp% - 10
goto shopcont
if %input%== 2
if %gp% LSS 5 goto nofunds
set /a gp= %gp% - 5
goto shopcont
if %input%== 3
if %gp% LSS 20 goto nofunds
set /a gp= %gp% - 20
goto shopcont
if %input%== 4
if %gp% LSS 1 goto nofunds
set /a gp= %gp% - 1
goto shopcont
goto shop

:nofunds
cls
echo You don't have enough gold to purchase that item.
pause >nul
goto shop

:shopcont
cls
echo Would you like to purchase anything else?
goto purchase

我还是新手,所以例子和解释会很棒!

请不要告诉我使用choice.exe而不是Set /P,除非它可以解决实际问题。

标签: windowsbatch-file

解决方案


在下面的示例中,我使用Set /Punder:purchase来满足您不使用choice.exe,的不良建议规定(我使用 under:shopcont代替)

:shop
ClS
Echo You see a middle aged man behind the shop counter, as well as a 
Echo younger man sweeping the floor.
Echo(
Echo "Welcome young travellers, is there anything I can help you with?"
:purchase
Set "input="
Set "invalid=true"
Echo(
Echo ------------------------------------------------------------------
Echo(%name%
Echo Gold: %gp%
Echo ------------------------------------------------------------------
Echo(
Echo 1. Battleaxe   10gp  [Stats: 1d8(S) Versatile(1d10)]
Echo 2. Mace         5gp  [Stats: 1d6(B)]
Echo 3. L.Crossbow  20gp  [Stats: 1d8(P) Range 80/320]
Echo 4. 5 Bolts      1gp  [Equip with Crossbow]
Echo 5. Go Back
Echo(
Set /P "input=Enter: "
For /L %%A In (1,1,5) Do If "%%~A" == "%input:"=%" Set "invalid="
If Defined invalid ClS & GoTo purchase
If %input% Equ 5 GoTo main
If %input% Equ 4 If %gp% GEq 1 Set /A gp -=1 & GoTo shopcont
If %input% Equ 3 If %gp% GEq 20 Set /A gp -=20 & GoTo shopcont
If %input% Equ 2 If %gp% GEq 5 Set /A gp -=5 & GoTo shopcont
If %input% Equ 1 If %gp% GEq 10 Set /A gp -=10 & GoTo shopcont
Echo You do not have enough gold to purchase that item.
:shopcont
"%__AppDir__%choice.exe" /M "Would you like to purchase anything else"
If "%ErrorLevel%"=="1" ClS & GoTo purchase

请注意,我试图复制您在问题中发布的内容,这假设%gp%并且%name%已经在此代码部分之前定义,并且标签:main存在于您未发布的代码中的其他位置。

您要求提供示例和解释,但这些在每个命令的使用信息和网络搜索下都很容易获得,所以我不会毫无意义地包含这些内容。


推荐阅读